【问题标题】:Multiply and groupby pandas python AttributeError: Cannot access callable attribute 'mul' of 'SeriesGroupBy' objects, try using the 'apply' methodMultiply and groupby pandas python AttributeError:无法访问“SeriesGroupBy”对象的可调用属性“mul”,请尝试使用“apply”方法
【发布时间】:2020-05-07 09:05:29
【问题描述】:

df

Person           Mul col
 A                   0
 B                   1 
 B                   1
 A                   0 

我想对 Person 进行 groupby 并使用 groupby 找到 Mul col 的乘法

试过了:

 df.groupby('Person')['Mul col'].mul()

错误

AttributeError: 无法访问“SeriesGroupBy”对象的可调用属性“mul”,请尝试使用“apply”方法

【问题讨论】:

  • 嗨@shailaja,不要忘记您可以投票并接受答案。见What should I do when someone answers my question?,谢谢!
  • 当然你好。实际上堆栈溢出不允许我在不等待 10 分钟的情况下接受答案,因此无法投票,后来我退出了。

标签: python pandas


【解决方案1】:

基本语法:

>>>df.groupby([col_name]).prod()

根据您的代码:

>>>df.groupby(['Person']).prod() # it will return dataframe from which we extract required col 

【讨论】:

    【解决方案2】:

    改用prod

    df.groupby('Person', as_index=False)['Mulcol'].prod()
    
         Person  Mulcol
    0      A       0
    1      B       1
    

    【讨论】:

      猜你喜欢
      • 2019-01-17
      • 1970-01-01
      • 2018-11-01
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多