在 Python Pandas 中查找 ID 的最小值、最大值和平均值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32965589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:59:02  来源:igfitidea点击:

Find min, max and average of an ID in Python Pandas

pythonpandas

提问by Tasos

I have two Dataframes in Python Pandas. Let's say that the first one is the df1. It is not necessary that the id column is ordered.

我在 Python Pandas 中有两个数据框。假设第一个是df1. 没有必要对 id 列进行排序。

   id  B  C
0   1  5  1
1   1  5  1
2   1  6  1
3   1  7  1
4   2  5  1
5   2  6  1
6   2  6  1
7   3  7  1
8   3  7  1
9   4  6  1
10  4  7  1
11  4  7  1

Then the second dataframe df2has a column with the unique values of the id

然后第二个数据帧df2有一个列的唯一值id

   id
0   1
1   2
2   3
3   4

I want to calculate the min, max and average of column Bfor each idand add it to the second dataframe. The result would be like this:

我想计算B每个列的最小值、最大值和平均值,id并将其添加到第二个数据帧中。结果是这样的:

   id  min  max  avg
0   1   5    7   5.75
1   2  ..
2   3  ..
3   4  ..

In this example, I was able to replicate it by calculating them for each idmanual. It was not a problem since the example has only 4 ids. But my real example has more than 1000 ids. Is there any automatic way to do it?

在这个例子中,我能够通过为每个id手册计算它们来复制它。这不是问题,因为该示例只有 4 个 ID。但是我的真实示例有 1000 多个 ID。有没有自动的方法来做到这一点?

回答by Zero

Use aggfunction on groups

agg对组使用函数

In [96]: df.groupby('id')['B'].agg([pd.np.min, pd.np.max, pd.np.mean])
Out[96]:
    amin  amax      mean
id
1      5     7  5.750000
2      5     6  5.666667
3      7     7  7.000000
4      6     7  6.666667