[英]Pandas Ranking based on column
New to Python here, I'm looking to create a column in my dataframe that ranks a column based off its value. Python 的新手,我希望在我的 dataframe 中创建一个列,根据其值对列进行排名。 Specifically in this situation, I have a balance field, and I want to rank the highest balance as 1, the second highest balance as 2, so on so forth.具体在这种情况下,我有一个余额字段,我想将最高余额列为 1,将第二高余额列为 2,依此类推。 However when I try to use the rank function, min or dense or any other option, it does not rank according to the balance... my attempt is below:但是,当我尝试使用等级 function、min 或 dense 或任何其他选项时,它不会根据余额进行排名......我的尝试如下:
import pandas as pd
# Create a test df
df = pd.DataFrame({'Name': ['Bob','Carl','Doug','Edith','Ford','George']
, 'Bank Amt': ['17','123','144','2','63','25']
, 'Loan Amt': ['147','1523','1144','542','5463','2135']
})
df['Bank Amt Rank'] = df['Bank Amt'].rank(method='min', ascending=True)
df
Output: Output:
Any help would be appreciated.任何帮助,将不胜感激。
your data is string
type, you need to convert to numerical type:您的数据是string
类型,您需要转换为数值类型:
df['Bank Amt'].astype(float).rank()
Output: Output:
0 2.0
1 5.0
2 6.0
3 1.0
4 4.0
5 3.0
Name: Bank Amt, dtype: float64
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.