Filter pandas Dataframe based on max values in a column

前端 未结 1 2037
执念已碎
执念已碎 2021-01-05 08:06

I have a DataFrame with repeating values in the index. I would like to filter this dataset down to only show me one instance of each index by selecting the row within the in

1条回答
  •  执笔经年
    2021-01-05 08:54

    You can perform a groupby on 'Product ID', then apply idxmax on 'Sales' column. This will create a series with the index of the highest values. We can then use the index values to index into the original dataframe using iloc

    In [201]:
    
    df.iloc[df.groupby('Product ID')['Sales'].agg(pd.Series.idxmax)]
    Out[201]:
       Product_ID Store  Sales
    1           1     B    200
    3           2     A    400
    5           3     A    200
    8           4     C    500
    

    0 讨论(0)
提交回复
热议问题