pandas.DataFrame.reindex_like#

DataFrame.reindex_like(other, method=None, copy=None, limit=None, tolerance=None)[源代码]#

返回具有与 other 对象匹配的索引的对象。

使对象在所有轴上都具有相同的索引。可选的填充逻辑,在没有先前索引值的位置放置 NaN。除非新索引等价于当前索引且 copy=False,否则会产生一个新对象。

Parameters:
other相同数据类型 else。

它的行和列索引用于定义该对象的新索引。

<strong>method</strong>{None, ‘backfill’/’bfill’, ‘pad’/’ffill’, ‘nearest’}

用于填充重索引 DataFrame 中缺失值的方法。请注意:这仅适用于索引单调递增/递减的 DataFrame/Series。

  • None(默认):不填充间隙

  • pad / ffill:将最后一个有效观测值向前传播到下一个有效值

  • backfill / bfill:使用下一个有效观测值填充间隙

  • nearest:使用最近的有效观测值填充间隙。

copybool, default True

返回一个新对象,即使传入的索引相同。

备注

copy 关键字在 pandas 3.0 中将更改行为。Copy-on-Write 将默认启用,这意味着所有带有 copy 关键字的方法都将使用惰性复制机制来延迟复制并忽略 copy 关键字。copy 关键字将在 pandas 的未来版本中移除。

通过启用 copy on write pd.options.mode.copy_on_write = True,您可以获得未来的行为和改进。

<strong>limit</strong>int,默认 None

对于非精确匹配,要填充的连续标签的最大数量。

toleranceoptional

原始标签和新标签之间的最大距离,用于非精确匹配。匹配位置的索引值必须满足方程 abs(index[indexer] - target) <= tolerance

tolerance 可以是标量值,它将相同的容差应用于所有值,也可以是类列表的值,它将可变容差应用于每个元素。类列表包括 list、tuple、array、Series,并且必须与索引大小相同,其 dtype 必须与索引的确切类型匹配。

Returns:
Series 或 DataFrame

与调用者相同类型,但每个轴的索引已更改。

参见

DataFrame.set_index

设置行标签。

DataFrame.reset_index

移除行标签或将它们移动到新列。

DataFrame.reindex

更改为新索引或扩展索引。

Notes

与调用 .reindex(index=other.index, columns=other.columns,...) 相同。

Examples

>>> df1 = pd.DataFrame([[24.3, 75.7, 'high'],
...                     [31, 87.8, 'high'],
...                     [22, 71.6, 'medium'],
...                     [35, 95, 'medium']],
...                    columns=['temp_celsius', 'temp_fahrenheit',
...                             'windspeed'],
...                    index=pd.date_range(start='2014-02-12',
...                                        end='2014-02-15', freq='D'))
>>> df1
            temp_celsius  temp_fahrenheit windspeed
2014-02-12          24.3             75.7      high
2014-02-13          31.0             87.8      high
2014-02-14          22.0             71.6    medium
2014-02-15          35.0             95.0    medium
>>> df2 = pd.DataFrame([[28, 'low'],
...                     [30, 'low'],
...                     [35.1, 'medium']],
...                    columns=['temp_celsius', 'windspeed'],
...                    index=pd.DatetimeIndex(['2014-02-12', '2014-02-13',
...                                            '2014-02-15']))
>>> df2
            temp_celsius windspeed
2014-02-12          28.0       low
2014-02-13          30.0       low
2014-02-15          35.1    medium
>>> df2.reindex_like(df1)
            temp_celsius  temp_fahrenheit windspeed
2014-02-12          28.0              NaN       low
2014-02-13          30.0              NaN       low
2014-02-14           NaN              NaN       NaN
2014-02-15          35.1              NaN    medium