python DataFrame类型数据排序问题

1、首先随机生成一个4*4数组,为DataFrame类型

import pandas as pd
import numpy as np
data = pd.DataFrame(np.random.randn(4,4),columns=['a','b','c','d'])
data
Out[24]:
  a b c d
0 -1.827002 -0.856608 0.396484 -0.060096
1 0.625663 -0.149912 -0.516815 0.470443
2 1.735904 -3.239465 0.382734 2.210866
3 0.578843 0.744071 -1.457105 0.188630

2、对 a 列 升序排列

data.sort_index(by='a',axis=0,ascending=True)
Out[46]:
  a b c d
0 -1.827002 -0.856608 0.396484 -0.060096
3 0.578843 0.744071 -1.457105 0.188630
1 0.625663 -0.149912 -0.516815 0.470443
2 1.735904 -3.239465 0.382734 2.210866

3、对 a 列 降序排列

data.sort_index(by='a',axis=0,ascending='False')
Out[62]:
  a b c d
2 1.735904 -3.239465 0.382734    2.210866
1 0.625663 -0.149912 -0.516815 0.470443
3 0.578843 0.744071 -1.457105 0.188630
0 -1.827002 -0.856608 0.396484 -0.060096


猜你喜欢

转载自blog.csdn.net/qq_21840201/article/details/80706040
今日推荐