[英]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.我有一个 Pandas 数据框,想找到按 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. df.groupby('id', as_index=False).agg(min)
会做到这一点。
id a b
1 0 5
2 4 1
You can use df.groupby('id').min()
您可以使用
df.groupby('id').min()
Result:结果:
a b
id
1 0 5
2 4 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.