文章目录
排序
对行或列索引进行排序(按字典序),可使用sort_index
方法,将返回一个已排序的新对象。
Series
obj=pd.Series(range(4),index=list('dabc'))
# d 0
# a 1
# b 2
# c 3
# dtype: int64
obj.sort_index()
# a 1
# b 2
# c 3
# d 0
# dtype: int64
DataFrame
frame=pd.DataFrame(np.arange(8).reshape(2,4),
index=['three','one'],
columns=list('dabc'))
|
d |
a |
b |
c |
three |
0 |
1 |
2 |
3 |
one |
4 |
5 |
6 |
7 |
frame.sort_index()
|
d |
a |
b |
c |
one |
4 |
5 |
6 |
7 |
three |
0 |
1 |
2 |
3 |
frame.sort_index(axis=1)
|
a |
b |
c |
d |
three |
1 |
2 |
3 |
0 |
one |
5 |
6 |
7 |
4 |
frame.sort_index(axis=1,ascending=False)
|
d |
c |
b |
a |
three |
0 |
3 |
2 |
1 |
one |
4 |
7 |
6 |
5 |
排名
Series
obj=pd.Series([7,-5,7,4,2,0,4])
# 默认给出平均排名
obj.rank()
# 0 6.5
# 1 1.0
# 2 6.5
# 3 4.5
# 4 3.0
# 5 2.0
# 6 4.5
# dtype: float64
# 根据出现顺序给出排名
obj.rank(method='first')
# 0 6.0
# 1 1.0
# 2 7.0
# 3 4.0
# 4 3.0
# 5 2.0
# 6 5.0
# dtype: float64
# 使用整个分组的最大排名,降序
obj.rank(ascending=False,method='max')
# 0 2.0
# 1 7.0
# 2 2.0
# 3 4.0
# 4 5.0
# 5 6.0
# 6 4.0
# dtype: float64
DataFrame
frame=pd.DataFrame({'b':[4.3,7,-3,2],'a':[0,1,0,1],'c':[-2,5,8,-2.5]})
|
b |
a |
c |
0 |
4.3 |
0 |
-2.0 |
1 |
7.0 |
1 |
5.0 |
2 |
-3.0 |
0 |
8.0 |
3 |
2.0 |
1 |
-2.5 |
frame.rank(axis=1) # 横轴排序
|
b |
a |
c |
0 |
3.0 |
2.0 |
1.0 |
1 |
3.0 |
1.0 |
2.0 |
2 |
1.0 |
2.0 |
3.0 |
3 |
3.0 |
2.0 |
1.0 |