代码之家  ›  专栏  ›  技术社区  ›  576i

如何将熊猫列与多索引数据帧的一部分相乘

  •  1
  • 576i  · 技术社区  · 8 年前

    我有一个带有多个索引和一列的数据帧。

    索引字段是 type amount ,列被调用 count

    我想添加一列 数量 计数

    df2 = df.groupby(['type','amount']).count().copy()
    # I then dropped all columns but one and renamed it to "count"
    
    df2['total_amount'] = df2['count'].multiply(df2['amount'], axis='index')
    

    不起作用。我有一个钥匙错误 数量 .

    如何访问多索引的一部分以在计算中使用它?

    1 回复  |  直到 8 年前
        1
  •  1
  •   jezrael    8 年前

    GroupBy.transform Series df multiple

    count = df.groupby(['type','amount'])['type'].transform('count')
    df['total_amount'] = df['amount'].multiply(count, axis='index')
    print (df)
       A  amount  C  D  E type  total_amount
    0  a       4  7  1  5    a             8
    1  b       5  8  3  3    a             5
    2  c       4  9  5  6    a             8
    3  d       5  4  7  9    b            10
    4  e       5  2  1  2    b            10
    5  f       4  3  0  4    b             4
    

    df = pd.DataFrame({'A':list('abcdef'),
                       'amount':[4,5,4,5,5,4],
                       'C':[7,8,9,4,2,3],
                       'D':[1,3,5,7,1,0],
                       'E':[5,3,6,9,2,4],
                       'type':list('aaabbb')})
    
    print (df)
       A  amount  C  D  E type
    0  a       4  7  1  5    a
    1  b       5  8  3  3    a
    2  c       4  9  5  6    a
    3  d       5  4  7  9    b
    4  e       5  2  1  2    b
    5  f       4  3  0  4    b
    
    df2 = df.groupby(['type','amount'])['type'].count().to_frame('count')
    df2['total_amount'] = df2['count'].mul(df2.index.get_level_values('amount'))
    print (df2)
                 count  total_amount
    type amount                     
    a    4           2             8
         5           1             5
    b    4           1             4
         5           2            10