Reputation: 1717
New to Python here, I'm looking to create a column in my dataframe that ranks a column based off its value. 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. 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:
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:
Any help would be appreciated.
Upvotes: 1
Views: 597
Reputation: 150735
your data is string
type, you need to convert to numerical type:
df['Bank Amt'].astype(float).rank()
Output:
0 2.0
1 5.0
2 6.0
3 1.0
4 4.0
5 3.0
Name: Bank Amt, dtype: float64
Upvotes: 3