python - pandas groupby、过滤器和聚合函数

标签 python pandas

我在 pandas 中有以下数据框

  key     time      outlier      
  1_2     4         False
  1_2     2         True
  1_2     2         True
  1_2     5         True
  1_2     6         False
  1_3     10        False
  1_3     12        False
  1_3     10        True
  1_3     20        True

我想计算离群值True的每个关键级别的平均值、中位数和计数。我想要的数据框如下

  key     time      outlier     outlier_avg      outlier_median     outlier_count    
  1_2     4         False       3                2                  3
  1_2     2         True        3                2                  3 
  1_2     2         True        3                2                  3
  1_2     5         True        3                2                  3
  1_2     6         False       3                2                  3
  1_3     10        False       15               15                 2
  1_3     12        False       15               15                 2
  1_3     10        True        15               15                 2
  1_3     20        True        15               15                 2

平均值、中位数和计数将在键之间重复,这很好。我正在 pandas 中做以下事情

  data.groupby('key').filter(lambda x : x['outlier'] == True).agg({'time':['mean','median', 'count'])

最佳答案

首先按boolean indexing过滤,然后聚合,将列名称重命名为 DataFrame.add_prefix最后DataFrame.join原文:

df = (data.join(data[data['outlier']].groupby('key')['time'].agg(['mean','median', 'count'])
                                     .add_prefix('outlier_'), on='key'))
print (df)
   key  time  outlier  outlier_mean  outlier_median  outlier_count
0  1_2     4    False             3               2              3
1  1_2     2     True             3               2              3
2  1_2     2     True             3               2              3
3  1_2     5     True             3               2              3
4  1_2     6    False             3               2              3
5  1_3    10    False            15              15              2
6  1_3    12    False            15              15              2
7  1_3    10     True            15              15              2
8  1_3    20     True            15              15              2

编辑:分别处理TrueFalse列的解决方案:

df = data.groupby(['key','outlier'])['time'].agg(['mean','median', 'count']).unstack()
df.columns = df.columns.map(lambda x: f'outlier_{x[0]}_{x[1]}')

df = data.join(df, on='key')
<小时/>
print (df)
   key  time  outlier  outlier_mean_False  outlier_mean_True  \
0  1_2     4    False                   5                  3   
1  1_2     2     True                   5                  3   
2  1_2     2     True                   5                  3   
3  1_2     5     True                   5                  3   
4  1_2     6    False                   5                  3   
5  1_3    10    False                  11                 15   
6  1_3    12    False                  11                 15   
7  1_3    10     True                  11                 15   
8  1_3    20     True                  11                 15   

   outlier_median_False  outlier_median_True  outlier_count_False  \
0                     5                    2                    2   
1                     5                    2                    2   
2                     5                    2                    2   
3                     5                    2                    2   
4                     5                    2                    2   
5                    11                   15                    2   
6                    11                   15                    2   
7                    11                   15                    2   
8                    11                   15                    2   

   outlier_count_True  
0                   3  
1                   3  
2                   3  
3                   3  
4                   3  
5                   2  
6                   2  
7                   2  

关于python - pandas groupby、过滤器和聚合函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59501215/

相关文章:

python - 如何在 rpi3 上通过 python 运行网络摄像头

python - 使用正则表达式以任意顺序匹配两个单词

python - 在 Sublime Text 插件中组织设置的方法

python - 如何在数据框中的列之间进行匹配并保留另一列

python - 通过从每一行的不同列中选择一个元素,从 Pandas DataFrame 创建一个系列

python - 如何从索引更改为多索引 - pandas

python - 在numpy中用3d数组索引2d数组

python - 感染效率 - Pygame

python - 将列标题转换为 Python 中的值和相应计数

python - Pandas 求和多个数据框