用 Python 编写一个程序来查找数据帧中特定列的最小排名
解决方案
假设您有一个数据框和特定列的最小排名,
Id Name Age Rank 0 1 Adam 12 1.0 1 2 David 13 3.0 2 3 Michael 14 5.0 3 4 Peter 12 1.0 4 5 William 13 3.0
为了解决这个问题,我们将按照下面给出的步骤-
定义一个数据框。
在秩函数中分配df['Age']列以计算轴0的最小秩为,
df["Age"].rank(axis=0,method ='min',ascending=True)
例子
让我们看下面的代码以获得更好的理解-
import pandas as pd
data = {'Id': [1,2,3,4,5],
'Name':["Adam","David","Michael","Peter","William"],
'Age': [12,13,14,12,13]}
df = pd.DataFrame(data)
df["Rank"] = df["Age"].rank(axis=0,method ='min',ascending=True)
print(df)输出
Id Name Age Rank 0 1 Adam 12 1.0 1 2 David 13 3.0 2 3 Michael 14 5.0 3 4 Peter 12 1.0 4 5 William 13 3.0
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短