df.reindex()和df.loc []之间的区别

时间:2014-04-09 18:21:17

标签: python pandas

假设我想计算两个MultiIndex数据帧之间的relative complement df2 - df1。假设它们具有相同的索引模式,基于我在Andy Hayden的this answer中看到的内容,我可以执行以下操作:

diff_indices = df2.index - df1.index

然后是:

  1. df2.reindex(diff_indices, inplace=True)

  2. df2 = df2.loc[diff_indices]

  3. 1.和2.上面的区别是什么? df.reindexdf.loc之间有什么区别?

1 个答案:

答案 0 :(得分:7)

这两种方法都会返回一个新的系列/数据帧,基本上也会做同样的事情。

看似冗余的原因是,虽然使用loc是语法上的限制(你只能将一个参数传递给__getitem__),reindex是一种支持服用的方法各种可选参数。 (docs