Supposed a dataframe
import pandas as pd
df = pd.DataFrame({
'Model': ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
'Year': [2019, 2020, 2021, 2018, 2019, 2020, 2021],
'Transmission': ['Manual', 'Automatic', 'Automatic', 'Manual', 'Automatic', 'Automatic', 'Manual'],
'EngineSize': [1.4, 2.0, 1.4, 1.5, 2.0, 1.5, 1.5],
'MPG': [55.4, 67.3, 58.9, 52.3, 64.2, 68.9, 83.1]
})
df
and I want to return the highest MPG per year plus the model. Looked like this
Year MPG Model
2018 52.3 D
2019 64.2 E
2020 68.9 F
2021 83.1 G
I'm thinking by using groupby but still stuck on how to show the Model column.