【问题标题】:average aggregation in pandas groupby while considering unique values of a columnpandas groupby 中的平均聚合,同时考虑列的唯一值
【发布时间】:2020-08-27 16:14:56
【问题描述】:

我有以下数据框:

df:
S0  S1  V1  V2  V3  V4
A   B   1   9   1   4
A   B   2   8   1   4
A   B   3   7   1   4
A   B   4   6   1   4
A   B   5   5   1   4
A   B   6   4   1   4
A   C   7   3   2   3
A   C   8   2   2   3
A   C   9   1   2   3
A   C   9   0   2   3

我在这里做一个 groupby 来聚合值并得到下表:

df:
S0  S1  V1  V2  V3  V4
A   B   21  39   1   4
A   C   33   6   2   3

V1 和 V2 列按总和进行聚合,而 V3 和 V4 列根据 S1 列的唯一值取平均值。

如果直接使用 Groupby 聚合,则不会考虑列 S1 的唯一值。这方面的前进方向是什么?

【问题讨论】:

  • 你能解释更多Groupby aggregation if used directly does not consider the column S1 for unique values. What can be the way forward for this?吗?

标签: python pandas pandas-groupby


【解决方案1】:

我相信您需要通过 S0S1 与聚合进行分组:

df1 = (df.groupby(['S0','S1'], as_index=False)
         .agg({'V1':'sum','V2':'sum','V3':'mean','V4':'mean'}))
print (df1)
  S0 S1  V1  V2  V3  V4
0  A  B  21  39   1   4
1  A  C  33   6   2   3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-03-05
    • 2019-10-02
    • 2019-06-05
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    相关资源
    最近更新 更多