I have a pandas data frame as follows
| Name | Marks latest |
|---|---|
| John | 32 |
| Sara | 43 |
| John | 45 |
| John | 82 |
| Sara | 69 |
What I want is a new data frame which only shows the final marks for each name:
| Name | Marks latest |
|---|---|
| John | 82 |
| Sara | 69 |
What's the easiest way to do so?
CodePudding user response:
Use drop_duplicates:
out = df.drop_duplicates('Name', keep='last')
print(out)
# Output
Name Marks latest
3 John 82
4 Sara 69
CodePudding user response:
You can use groupby()
df.groupby('Name', as_index = False)['Marks latest'].agg('last')
