Pandas中有不少操作与索引相关,非常有必要缕清它们之间的区别与联系,方便记忆与使用。

1. set_index

顾名思义,设置索引。可以为列表形式,设置多重索引。常用关键字是inplace和append。

2. reset_index

官方文档是这样介绍该函数的功能的,As a convenience, there is a new function on DataFrame called reset_index() which transfers the index values into the DataFrames columns and  sets a simple integer index. This is the inverse operation of set_index()。即将原来的index列设置为普通列,与set_index()方法的功能相反。

3. reindex

重新排列索引顺序。加columns关键字,可重新排列列索引顺序。如果索引值在原索引中不存在,则默认填充为NaN值。

重排索引之后,用ffill()或bfill()函数,可实现前向填充或后向填充。reindex中的method关键字参数不建议再使用了。

4. reindex_like

Dataframe.reindex_like(other,…)

去学习另外一个的df对象的index与columns,跟这个效果一样reindex(index=other.index, columns=other.columns,...)