I have removed some rows from my dataframe and therefore need to reset the rank column. I have sorted the rank column in descending order and now need to redo the numbering. Any suggestions?
Current df:
Country Rank
GB 1
SGP 3
CHE 4
USA 8
IRL 9
Desired df:
Country Rank
GB 1
SGP 2
CHE 3
USA 4
IRL 5
You can use below code:
df['Rank'] = range(1, 1+len(df))
import pandas as pd
data = {'Country': ['GB', 'SGP', 'CHE', 'USA', 'IRL'], 'Rank': [1, 3, 4, 8, 9],}
df = pd.DataFrame(data)
'''You can generate a list of intger range, from 1 to the length of you dataframe + 1, then overwrite your rank column with it'''
df['Rank'] = range(1,len(df)+1)
print(df)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.