【问题标题】:Pandas dataframe Groupby with Min,Max and Sum带有最小值、最大值和总和的 Pandas 数据框 Groupby
【发布时间】:2021-08-01 07:10:05
【问题描述】:

我在 AID 级别有以下数据框,我想 Group By 在 CID 上使用 min 优先级,max Ind 值,并计算金额字段的 sum

数据框

AID   CID   priority amount    Ind
100   C100     1       50       1
200   C100     2       100      0
300   C300     5       300      0
400   C300     3       200      0
500   C300     4       150      0

所需的数据帧

CID  Priority   amount   Ind
C100    1       150       1
C300    3       650       0

我试过了

df2=df.groupby(['CID']).min('Priority')

Error: method object is not subscriptable

【问题讨论】:

    标签: pandas group-by sum min


    【解决方案1】:
    print(
        df.groupby("CID", as_index=False).agg(
            {"priority": "min", "Ind": "max", "amount": "sum"}
        )
    )
    

    打印:

        CID  priority  Ind  amount
    0  C100         1    1     150
    1  C300         3    0     650
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      • 2017-07-25
      • 1970-01-01
      • 2021-07-20
      相关资源
      最近更新 更多