Home > Enterprise >  Python Grouping - list only the minimum value row
Python Grouping - list only the minimum value row

Time:03-31

I am having an issue with what I can only assume is a simple issue, if anyone might be able to help out with where I have gone wrong I would be most grateful.

Below is the solution I have found searching on stack - unfortunately this isn't what I an looking for:

p2all.loc[p2all.groupby('emotion').age.idxmin()]

Below is my code, which is almost working as it should but not quite:

p2all.groupby(['emotion']).emotion.agg(['count'])

my query gives these results:

All I am now looking to do is only show the minimum value which is Euphoric 658 and none of the others, but after 2 hours of google and stack, I am either completely missing it, or I am searching the incorrect things.

Oddly this is something I can do easily in SQL :/

Please help and go easy on me, this is my first post.

Best regards

O

CodePudding user response:

The resulting object after using groupby aggregate function is a series. You can simply use series functions to get min or max or whatever you want.

series = p2all.groupby('emotion')['emotion'].agg('count')


min_idx = series.idxmin()

min_val = series.loc[min_idx]


print(f" emotion: {min_idx}, value: {min_val}")

(min_idx, min_val) is what you are looking for.

  • Related