简体   繁体   English

如何为pandas数据框中按ID分组的每一列找到每个唯一值的最小值

[英]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.

相关问题 从某个范围内的列中找到一个固定值,并在熊猫数据框中找到另一列的每个唯一值 - find a fix value from a column around a range with each unique values of another column in pandas data frame pandas 循环遍历列中每个唯一值的数据帧 - pandas loop through data frame for each unique value in column 如何找到按月分组的 pandas 数据帧中的最高中值? - How to find the highest median value in pandas data frame grouped by month? 熊猫-为分组数据中的每个组分配唯一ID - Pandas - Assign unique ID to each group in grouped data Python / Pandas:如何在数据框中的每一列中查找特定值的出现次数? - Python/Pandas: How to find the number of occurrences of a specific value in each column a data frame? 如何用列值替换pandas数据框中的每个值? - How to replace each value in pandas data frame with column value? 如何在每列的熊猫数据框中找到不合适的数据类型? - How to find inappropriate datatype in pandas data frame for each column? 如何有效地选择按索引熊猫分组的列中的最小值? - How to efficiently select the minimum value in a column grouped by index pandas? 是否有熊猫函数来转置数据框以为现有列的每个唯一值创建单独的列? - Is there a pandas function to transpose a data frame to create a separate column for each unique value of an existing column? 如果另一列中的值重复,如何在 pandas 数据框中获取最小列值? - How to take minimum column value in pandas data frame if values in another column repeat?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM