Pandas 模块-操纵数据(2)-重新索引-reindex()函数

2. 重新索引

请注意,DataFrame.rename() 函数是对行名和列名进行修改,并不修改数据,而DataFrame.reindex 可以引入新行/列,或者去掉旧行/列。

2.1 reindex() 函数

2.1.1 reindex() 语法

DataFrame.reindex(self, labels=None, index=None, columns=None, axis=None, method=None, copy=True, level=None, fill_value=nan, limit=None, tolerance=None) 

使用可选的填充逻辑使DataFrame符合新索引。

  • labels : 类似数组,可选,新 labels / index "axis"指定的轴与之一致。
  • index, columns : 类似数组,可选;要使用的新labels/index 引要符合。最好是一个Index对象,以避免重复数据。
  • axis: 默认是 index
  • method : {默认 None,'backfill'/'bfill,'pad'/'ffill’, 'nearest'}, 

        用于在重新索引的DataFrame中填充孔的方法。

        请注意:这仅适用于具有单调递增/递减索引的DataFrames/Series。

        1) None (default): 不填补空白

        2) pad / ffill: 将上一个有效观察值向前传播到下一个有效值。

        3) backfill / bfill: 使用下一个有效观察值填充空白。

        4) nearest: 使用最近的有效观测值来填补空白。

  • copy : boolean, 默认 True,即使传递的索引相同,也返回一个新对象。
  • level : int 或 name,在一个级别上广播,在传递的MultiIndex级别上匹配索引值。
  • fill_value : scalar, 默认为 np.NaN,用于缺失值的值。默认为NaN,但可以是任何“compatible”值。
  • limit : int, 默认 None。向前或向后填充的连续元素的最大数量。
  • tolerance: 可选。不精确匹配的原始标签和新标签之间的最大距离。

        在匹配位置的索引值最符合公式

        abs(index[indexer] - target) <= tolerance。

        公差可以是一个标量值,它对所有值应用相同的tolerance;

        也可以是类似列表的值,它对每个元素应用可变的tolerance。

        list-like包括list、tuple、array、Series,

        并且必须与索引相同大小,其 dtype 必须与索引的类型完全匹配。

Help on function reindex in module pandas.core.frame:

reindex(self, labels=None, index=None, columns=None, axis=None, method=None, copy=True, level=None, fill_value=nan, limit=None, tolerance=None)
    Conform Series/DataFrame to new index with optional filling logic.
    
    Places NA/NaN in locations having no value in the previous index. A new object
    is produced unless the new index is equivalent to the current one and
    ``copy=False``.
    
    Parameters
    ----------
    
    keywords for axes : array-like, optional
        New labels / i
  • 14
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江南野栀子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值