Pandas---重置行/列索引
·
重置列索引
新写一个列表,里面为新的列顺序
a = pd.DataFrame(np.arange(12).reshape(4,3),columns=['A','B','C'])
输出为
A B C
0 0 1 2
1 3 4 5
2 6 7 8
3 9 10 11
# 新的列索引列表:
new_col_list = ['C','B','A']
a = a[new_col_list]
C B A
0 2 1 0
1 5 4 3
2 8 7 6
3 11 10 9
重置行索引
使用reset_index()
a = pd.DataFrame(np.arange(12).reshape(6,2),columns=['A','B'],index=np.random.randint(0,20,6))
A B
0 0 1
5 2 3
15 4 5
18 6 7
7 8 9
12 10 11
# 使用reset_index()
# 将索引重置,并不把原索引加入
a.reset_index(drop=True,inplace=True)
A B
0 0 1
1 2 3
2 4 5
3 6 7
4 8 9
5 10 11
更多推荐



所有评论(0)