简体   繁体   中英

Python - Using groupby and conditional statements

How would you go about grouping a DataFrame based on one column, checking which is the highest value on another column within each group (or any other condition), and creating a new DataFrame with the rows of all values found during the grouping?

If I use groupby on the column of interest to then check the condition I want, the only thing I'll get back is a df with two columns, but I want to all columns of the original df.

Code:

df_best = df.groupby('Type 1')['Total'].max()

I know why my code above doesn't work in this case, but I can't figure out a way to get what I want...

Thanks in advance!

What I finally did was:

best = df[df['Total'] == df.groupby('Type 1')['Total'].transform('max')]
best = best.sort_values('Type 1')

As ALollz commented, this finds all rows in the original df (after applying the groupby) that are equal to the condition established (in this case 'max'), and returns a new df based on the results.

ALollz, thanks a lot!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM