2020-04-30
阅读量:
4593
dataframe索引重排序,题目详情见正文
df表如下:

要求根据已有的索引顺序("002","005","004","001","006","003")更改df的索引,实现效果如下:

参考代码:
np.random.seed(0)
B = pd.Series(["Allen","Lucy","Tom","Alice","Tim","Lily"], dtype="string")
C = pd.Series(np.random.randint(20,40,6, dtype="int"))
df = pd.DataFrame({'name':B, 'sale':C})
df.index = ["001","002","003","004","005","006"]
df
index1 = ["002","005","004","001","006","003"]
df.reindex(index1)
代码结果:







推荐帖子
2条评论
6条评论
7条评论