使用 Python 对列中每个唯一值求和、最大值和平均值

vol*_*kan 2 python function pandas

我有一个像这样的熊猫数据框:

df = pd.DataFrame({'date': [1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 7, 7],
                   'machine': ['a', 'b', 'a', 'b', 'a', 'b', 'a', 'b', 'c', 'a', 'b', 'e', 'a', 'b'],
                   'meters': [12, 9, 7, 9, 4, 9, 3, 7, 12, 9, 7, 9, 4, 9]},
)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

通过一个函数,对于“machine”列中的每个唯一值,我想自动打印如下句子:

  • 对于一台机器来说总和是 39

    对于机器来说平均值是 6.5

    对于机器最大为 12

  • 对于b机器来说总和是50

    对于 b 机均值是 8.3

    对于 b 机器,最大值为 9

  • 对于c机器来说总和是12

    对于c机器来说平均值是12

    对于 c 机器最大为 12

  • 对于 e 机来说总和是 9

    对于 e 机均值是 9

    对于 e 机最大为 9

基本上我该如何写一个定义?

Rom*_*est 5

对每组进行分组machine并求和:meters

for m, s in df.groupby('machine')['meters'].sum().items():
    print(f'For {m} machine sum is {s}')
Run Code Online (Sandbox Code Playgroud)
For a machine sum is 39
For b machine sum is 50
For c machine sum is 12
For e machine sum is 9
Run Code Online (Sandbox Code Playgroud)

UPD:(由于扩展要求)

对于更扩展的聚合,请使用以下方法(带有应用.agg函数):

for m, d in df.groupby('machine')['meters'].agg(['sum', 'mean', 'max']).to_dict('index').items():
    print(f'For {m} machine: sum is {d["sum"]}, '
          f'mean is {d["mean"]}, max is {d["max"]}')
Run Code Online (Sandbox Code Playgroud)
For a machine: sum is 39, mean is 6.5, max is 12
For b machine: sum is 50, mean is 8.333333333333334, max is 9
For c machine: sum is 12, mean is 12.0, max is 12
For e machine: sum is 9, mean is 9.0, max is 9
Run Code Online (Sandbox Code Playgroud)