如何为 pandas 数据框中按ID分组的每一列查找每个唯一值的最小值 [英] How to find minimum for each unique value for every column grouped by ID in pandas data frame
本文介绍了如何为 pandas 数据框中按ID分组的每一列查找每个唯一值的最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个pandas数据框,想找到按ID分组的每一列的最小值.
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)
会做到这一点.
id a b
1 0 5
2 4 1
这篇关于如何为 pandas 数据框中按ID分组的每一列查找每个唯一值的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文