Pandas详解十二之排序和排名

约定:
import pandas as pd
import numpy as np

排序和排名

根据条件对Series对象或DataFrame对象的值排序(sorting)排名(ranking)是一种重要的内置运算。
接下来为大家介绍如何使用pandas对象的:sort_index() / sort_values() / rank() 方法。

一、对Series排序

对Series对象排序是最常用的,可以根据Series对象的索引排序。

  • 根据索引排序
se1=pd.Series(np.arange(10,13),index=[1,3,2])
se1.sort_index()

代码结果:

1    10
2    12
3    11
dtype: int32
  • 还能对字符索引排序
se2=pd.Series(np.arange(0,3),index=['c','d','a'])
se2.sort_index()

代码结果:

a    2
c    0
d    1
dtype: int32
  • 降序排序
se2.sort_index(ascending=False)

代码结果:

d    1
c    0
a    2
dtype: int32
  • 排序
se3=pd.Series([3,-5,7])
se3.sort_values()

代码结果:

1   -5
0    3
2    7
dtype: int64
  • NaN值会放在Series末尾
se4=pd.Series([3,np.nan,-7,np.nan,5])
se4.sort_values()

代码结果:

2   -7.0
0    3.0
4    5.0
1    NaN
3    NaN
dtype: float64

二、对DataFrame排序

  • 通过axis参数可以对任意轴排序
df1=pd.DataFrame(np.arange(9).reshape(3,3),index=list("bac"),columns=list("yzx"))
df1
代码结果:
y z x
b 0 1 2
a 3 4 5
c 6 7 8
df1.sort_index()
代码结果:
y z x
a 3 4 5
b 0 1 2
c 6 7 8
df1.sort_index(axis=1)
代码结果:
x y z
b 2 0 1
a 5 3 4
c 8 6 7
  • 根据一个列的值来排序
df2=pd.DataFrame({'a':[20,3,3],'b':[1,-6,18]})
df2.sort_values(by='b')
代码结果:
a b
1 3 -6
0 20 1
2 3 18
  • 对多个列来排序
df2.sort_values(by=['a','b'])
代码结果:
a b
1 3 -6
2 3 18
0 20 1

三、排名

排名是根据Series对象或DataFrame的某几列的值进行排名,.rank(method=,ascending=,…)返回对值的排名。但需要十分注意如何处理出现相同的值。

  • 平均排名

为相同的值分配一个平均排名

se5=pd.Series([2,3,7,5,3,7])
se5.rank()

代码结果:

0    1.0
1    2.5
2    5.5
3    4.0
4    2.5
5    5.5
dtype: float64
  • 顺序排名

对于相同的值按照出现的顺序排名

se5.rank(method="first")

代码结果:

0    1.0
1    2.0
2    5.0
3    4.0
4    3.0
5    6.0
dtype: float64
  • 最小值排名

对于相同的值都取小的排名

se5.rank(method="min",ascending=False)

代码结果:

0    6.0
1    4.0
2    1.0
3    3.0
4    4.0
5    1.0
dtype: float64
  • 最大值排名

对于相同的值都取大的排名

se5.rank(method="max",ascending=False)

代码结果:

0    6.0
1    5.0
2    2.0
3    3.0
4    5.0
5    2.0
dtype: float64
  • 降序排名
se5.rank(method="first",ascending=False)

代码结果:

0    6.0
1    4.0
2    1.0
3    3.0
4    5.0
5    2.0
dtype: float64

谢谢大家的浏览,
希望我的努力能帮助到您,
共勉!

猜你喜欢

转载自blog.csdn.net/weixin_38168620/article/details/80070272