pandas.Series.reindex #
- 系列。reindex ( index = None , * , axis = None , method = None , copy = None , level = None , fill_value = None , limit = None , tolerance = None ) [来源] #
通过可选的填充逻辑使系列符合新索引。
将 NA/NaN 放置在前一个索引中没有值的位置。除非新索引等于当前索引 和 ,否则将生成一个新对象
copy=False
。- 参数:
- 类似索引数组,可选
索引的新标签。最好是 Index 对象以避免重复数据。
- axis int 或 str,可选
没用过。
- 方法{无,'backfill'/'bfill','pad'/'ffill','最近'}
用于填充重新索引的 DataFrame 中的漏洞的方法。请注意:这仅适用于具有单调递增/递减索引的 DataFrames/Series。
None(默认):不填补空白
pad / fill:将最后一个有效观察向前传播到下一个有效观察。
backfill / bfill:使用下一个有效观察来填补空白。
最近的:使用最近的有效观察来填补空白。
- 复制布尔值,默认 True
返回一个新对象,即使传递的索引相同。
笔记
copy关键字将改变 pandas 3.0 中的行为 。默认情况下会启用Copy-on-Write ,这意味着所有带有 copy关键字的方法都将使用惰性复制机制来推迟复制并忽略copy关键字。 copy关键字将在 pandas 的未来版本中删除。
您已经可以通过启用写入时复制来获得未来的行为和改进
pd.options.mode.copy_on_write = True
- level整数或名称
跨级别广播,匹配传递的多索引级别上的索引值。
- fill_value标量,默认 np.nan
用于缺失值的值。默认为 NaN,但可以是任何“兼容”值。
- limit int,默认无
向前或向后填充的连续元素的最大数量。
- 公差可选
不精确匹配的原始标签和新标签之间的最大距离。匹配位置处的索引值最满足方程。
abs(index[indexer] - target) <= tolerance
公差可以是标量值,它对所有值应用相同的公差,也可以是类似列表的,它对每个元素应用可变的公差。类列表包括列表、元组、数组、系列,并且必须与索引大小相同,并且其数据类型必须与索引的类型完全匹配。
- 返回:
- 索引已更改的系列。
也可以看看
DataFrame.set_index
设置行标签。
DataFrame.reset_index
删除行标签或将其移动到新列。
DataFrame.reindex_like
更改为与其他 DataFrame 相同的索引。
例子
DataFrame.reindex
支持两种调用约定(index=index_labels, columns=column_labels, ...)
(labels, axis={'index', 'columns'}, ...)
我们强烈建议使用关键字参数来阐明您的意图。
使用一些虚构数据创建一个数据框。
>>> index = ['Firefox', 'Chrome', 'Safari', 'IE10', 'Konqueror'] >>> df = pd.DataFrame({'http_status': [200, 200, 404, 404, 301], ... 'response_time': [0.04, 0.02, 0.07, 0.08, 1.0]}, ... index=index) >>> df http_status response_time Firefox 200 0.04 Chrome 200 0.02 Safari 404 0.07 IE10 404 0.08 Konqueror 301 1.00
创建一个新索引并重新索引数据帧。默认情况下,会分配新索引中在数据帧中没有对应记录的值
NaN
。>>> new_index = ['Safari', 'Iceweasel', 'Comodo Dragon', 'IE10', ... 'Chrome'] >>> df.reindex(new_index) http_status response_time Safari 404.0 0.07 Iceweasel NaN NaN Comodo Dragon NaN NaN IE10 404.0 0.08 Chrome 200.0 0.02
我们可以通过将值传递给关键字来填充缺失的值
fill_value
。由于索引不是单调递增或递减,因此我们不能使用关键字的参数来method
填充NaN
值。>>> df.reindex(new_index, fill_value=0) http_status response_time Safari 404 0.07 Iceweasel 0 0.00 Comodo Dragon 0 0.00 IE10 404 0.08 Chrome 200 0.02
>>> df.reindex(new_index, fill_value='missing') http_status response_time Safari 404 0.07 Iceweasel missing missing Comodo Dragon missing missing IE10 404 0.08 Chrome 200 0.02
我们还可以重新索引列。
>>> df.reindex(columns=['http_status', 'user_agent']) http_status user_agent Firefox 200 NaN Chrome 200 NaN Safari 404 NaN IE10 404 NaN Konqueror 301 NaN
或者我们可以使用“axis-style”关键字参数
>>> df.reindex(['http_status', 'user_agent'], axis="columns") http_status user_agent Firefox 200 NaN Chrome 200 NaN Safari 404 NaN IE10 404 NaN Konqueror 301 NaN
为了进一步说明 中的填充功能
reindex
,我们将创建一个具有单调递增索引的数据框(例如,日期序列)。>>> date_index = pd.date_range('1/1/2010', periods=6, freq='D') >>> df2 = pd.DataFrame({"prices": [100, 101, np.nan, 100, 89, 88]}, ... index=date_index) >>> df2 prices 2010-01-01 100.0 2010-01-02 101.0 2010-01-03 NaN 2010-01-04 100.0 2010-01-05 89.0 2010-01-06 88.0
假设我们决定扩展数据框以覆盖更广泛的日期范围。
>>> date_index2 = pd.date_range('12/29/2009', periods=10, freq='D') >>> df2.reindex(date_index2) prices 2009-12-29 NaN 2009-12-30 NaN 2009-12-31 NaN 2010-01-01 100.0 2010-01-02 101.0 2010-01-03 NaN 2010-01-04 100.0 2010-01-05 89.0 2010-01-06 88.0 2010-01-07 NaN
原始数据框中没有值的索引条目(例如“2009-12-29”)默认填充为
NaN
。如果需要,我们可以使用多个选项之一来填充缺失值。例如,要反向传播最后一个有效值来填充值
NaN
,请将其bfill
作为参数传递给method
关键字。>>> df2.reindex(date_index2, method='bfill') prices 2009-12-29 100.0 2009-12-30 100.0 2009-12-31 100.0 2010-01-01 100.0 2010-01-02 101.0 2010-01-03 NaN 2010-01-04 100.0 2010-01-05 89.0 2010-01-06 88.0 2010-01-07 NaN
请注意,
NaN
原始数据帧中存在的值(索引值 2010-01-03)不会由任何值传播方案填充。这是因为重新索引时填充不会查看数据帧值,而只会比较原始索引和所需索引。如果您确实想填写NaN
原始数据框中存在的值,请使用该fillna()
方法。请参阅用户指南了解更多信息。