Use tail
:
df.groupby("ID").tail(1)
The output is:
ID Year Var1 Var2
1 1 2019 b f
2 2 2019 c g
3 3 2020 d h
Another alternative is to use last
:
df.groupby("ID").last()
Use drop_duplicates:
df.sort_values('Year').drop_duplicates('ID', keep='last')
Output:
ID Year Var1 Var2
1 1 2019 b f
2 2 2019 c g
3 3 2020 d h
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.