简体   繁体   中英

How to find minimum for each unique value for every column grouped by ID in pandas data frame

I have a pandas dataframe and would like to find minimum value for each column grouped by ID.

#Input data 

df=pd.DataFrame({ 'id':[1,1,1,1,2,2,2,2],
                   'a':range(8), 'b':range(8,0,-1) })

#expected output is the minimum value for each id and column (a, b)
id  a    b  
1   0    5
2   4    1   

df.groupby('id', as_index=False).agg(min) will do just that.

id  a  b
1   0  5
2   4  1

You can use df.groupby('id').min()

Result:

    a  b
id      
1   0  5
2   4  1

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