python - Filter pandas Dataframe based on max values in a column -
I have a dataframe with values repetitive in the index. I have used only one index to filter this dataset below I show an example by selecting the row in the index with the largest value in a different column. For example, my datafree looks like this:
df:
Product ID Store Sale 1A 50 1B 200 1C 202A400BB103 A 200 4A 50 4B 100 4C500
I would like to filter this data on:
df2:
Product ID Store Sales 1B 200 2A 400 3A 200 4C500
Any ideas on how best to reach this issue in pandas?
Thank you Your Time -
You can add a Group,
, then apply the idxmax
to the 'Sales' column. It will prepare a series with the highest values index. We can then use the index code in the original dataframe in the iloc
[201]: df.iloc [df.groupby ('Product ID') [ 'Sales']. AGG (Pdseries idxmx)] Out [201]: Product_ID Store Sale 1 1B 200 3 2A 400 5 3A 200 8 4C500
Comments
Post a Comment