Groupby Pandas数据框并计算一列的平均值和标准差

bqujaahr  于 2023-01-28  发布在  其他
关注(0)|答案(2)|浏览(248)

我有一个Pandas数据框如下:

  1. a b c d
  2. 0 Apple 3 5 7
  3. 1 Banana 4 4 8
  4. 2 Cherry 7 1 3
  5. 3 Apple 3 4 7

我想按列“a”对行进行分组,同时用分组行中的平均值替换列“c”中的值,并添加另一列,该列的平均值已计算为列“c”中的值的标准差。列“b”或“d”中的值对于分组的所有行都是常数。因此,所需的输出将是:

  1. a b c d e
  2. 0 Apple 3 4.5 7 0.707107
  3. 1 Banana 4 4 8 0
  4. 2 Cherry 7 1 3 0

实现这一目标的最佳途径是什么?

p8h8hvxi

p8h8hvxi1#

您可以使用groupby-agg操作:

  1. In [38]: result = df.groupby(['a'], as_index=False).agg(
  2. {'c':['mean','std'],'b':'first', 'd':'first'})

然后重命名并重新排序列:

  1. In [39]: result.columns = ['a','c','e','b','d']
  2. In [40]: result.reindex(columns=sorted(result.columns))
  3. Out[40]:
  4. a b c d e
  5. 0 Apple 3 4.5 7 0.707107
  6. 1 Banana 4 4.0 8 NaN
  7. 2 Cherry 7 1.0 3 NaN

默认情况下,Pandas计算样本标准差。要计算总体标准差:

  1. def pop_std(x):
  2. return x.std(ddof=0)
  3. result = df.groupby(['a'], as_index=False).agg({'c':['mean',pop_std],'b':'first', 'd':'first'})
  4. result.columns = ['a','c','e','b','d']
  5. result.reindex(columns=sorted(result.columns))

收益率

  1. a b c d e
  2. 0 Apple 3 4.5 7 0.5
  3. 1 Banana 4 4.0 8 0.0
  4. 2 Cherry 7 1.0 3 0.0
展开查看全部
lokaqttq

lokaqttq2#

如果某些列中的值对于分组的所有行都是常量(例如OP中的“b”、“d”),则可以将其包含到分组器中,并在以后重新排序列。

  1. new_df = (
  2. df.groupby(['a', 'b', 'd'])['c'].agg(['mean', 'std']) # groupby operation
  3. .set_axis(['c', 'e'], axis=1) # rename columns
  4. .reset_index() # make groupers into columns
  5. [['a', 'b', 'c', 'd', 'e']] # reorder columns
  6. )

您还可以使用命名聚合使groupby结果具有自定义列名。mean列命名为'c'std列命名为groupby.agg末尾的'e'

  1. new_df = (
  2. df.groupby(['a', 'b', 'd'])['c'].agg([('c', 'mean'), ('e', 'std')])
  3. .reset_index() # make groupers into columns
  4. [['a', 'b', 'c', 'd', 'e']] # reorder columns
  5. )

您也可以将参数传递给groupby.agg。例如,如果您需要在groupby.agg中将ddof=0传递给std(),则可以使用lambda来完成此操作。

  1. new_df = (
  2. df.groupby(['a', 'b', 'd'])['c'].agg([('c', 'mean'), ('e', lambda g: g.std(ddof=0))])
  3. .reset_index()[['a', 'b', 'c', 'd', 'e']]
  4. )

展开查看全部

相关问题