Home > Mobile >  Reduces dataframe based on maximum and duplicate rows [duplicate]
Reduces dataframe based on maximum and duplicate rows [duplicate]

Time:09-24

I would like to reduce this dataframe with multiple rows with the same name, but with different values to a dataframe containing only 1 row per name with the maximum value.

df = pd.DataFrame([{"Name": "Foo", "Value": 1}, {"Name": "Foo", "Value": 3}, {"Name": "Bar", "Value": 1},{"Name": "Bar", "Value": 4}])

Dataframe wanted:

df_filtered = pd.DataFrame([{"Name": "Foo", "Value": 3}, {"Name": "Bar", "Value": 4}])

I started with df.groupby however it didn't feel natural.

CodePudding user response:

Sort values to have the max values at the end, then take the last row per group.

df.sort_values(by='Value').groupby('Name', as_index=False).last()
  • Related