Increasing Rank based on year in pandas dataframe
Question:
I have three dataframes for year 2022,year 2021 and year 2020 as given below:
ID YEAR RANK
500 2022 1
510 2022 2
520 2022 3
ID YEAR RANK
501 2021 1
550 2021 2
560 2021 3
ID YEAR RANK
505 2020 1
506 2020 2
507 2020 3
I have to append year 2020 dataframe below year 2021 dataframe and 2022 dataframe needs to be at top and reassign the rank as given in below output:
ID YEAR RANK
500 2022 1
510 2022 2
520 2022 3
501 2021 4
550 2021 5
560 2021 6
505 2020 7
506 2020 8
507 2020 9
In above output I need to reassign the rank in a way that rank for year 2022 dataframe needs to be same but for first Id of year 2021 rank needs to be 4 which 1 higher than highest rank for year 2022 and so on. Similarly for year 2020 if highest rank for year 2021 is 6 rank for first Id for year 2020 rank needs to be 7 and so on.
Answers:
Add maximal rank to all ranks of df1
first and then use concat
:
df2 = df2.assign(RANK = df2.RANK + df1.RANK.max())
df = pd.concat([df1, df2], ignore_index=True)
print (df)
ID YEAR RANK
0 500 2022 1
1 510 2022 2
2 520 2022 3
3 501 2021 4
4 550 2021 5
5 560 2021 6
I have three dataframes for year 2022,year 2021 and year 2020 as given below:
ID YEAR RANK
500 2022 1
510 2022 2
520 2022 3
ID YEAR RANK
501 2021 1
550 2021 2
560 2021 3
ID YEAR RANK
505 2020 1
506 2020 2
507 2020 3
I have to append year 2020 dataframe below year 2021 dataframe and 2022 dataframe needs to be at top and reassign the rank as given in below output:
ID YEAR RANK
500 2022 1
510 2022 2
520 2022 3
501 2021 4
550 2021 5
560 2021 6
505 2020 7
506 2020 8
507 2020 9
In above output I need to reassign the rank in a way that rank for year 2022 dataframe needs to be same but for first Id of year 2021 rank needs to be 4 which 1 higher than highest rank for year 2022 and so on. Similarly for year 2020 if highest rank for year 2021 is 6 rank for first Id for year 2020 rank needs to be 7 and so on.
Add maximal rank to all ranks of df1
first and then use concat
:
df2 = df2.assign(RANK = df2.RANK + df1.RANK.max())
df = pd.concat([df1, df2], ignore_index=True)
print (df)
ID YEAR RANK
0 500 2022 1
1 510 2022 2
2 520 2022 3
3 501 2021 4
4 550 2021 5
5 560 2021 6