【问题标题】:Pandas named aggregation syntax throws error TypeError: int() argument must be a string, a bytes-like object or a number, not '_NoValueType'Pandas 命名聚合语法抛出错误 TypeError: int() argument must be a string, a bytes-like object or a number, not '_NoValueType'
【发布时间】:2021-12-13 06:37:49
【问题描述】:

我有一个如下的熊猫数据框

aa = pd.DataFrame({
'x': range(0, 5),
'g1': [1,1,2,2,1]})

当我使用未命名的聚合时,我得到了预期的结果

xx = aa.groupby('g1').agg({'x' : 'sum'}).reset_index()

但是当我将语法更改为命名聚合时

xx = aa.groupby('g1').agg(xsum=('x', 'sum')).reset_index()

我收到以下错误消息

TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是 '_NoValueType'

不知道哪里出错了..

【问题讨论】:

    标签: python pandas aggregation


    【解决方案1】:

    您的错误出在其他地方,您的代码中没有转换为 int。

    提供的 sn-p 工作正常,正如预期的那样:

    import pandas as pd
    aa = pd.DataFrame({'x': range(0, 5),
                       'g1': [1,1,2,2,1]})
    aa.groupby('g1').agg(xsum=('x', 'sum')).reset_index()
    

    输出:

       g1  xsum
    0   1     5
    1   2     5
    

    【讨论】:

      猜你喜欢
      • 2022-11-11
      • 2021-02-02
      • 2019-12-29
      • 2021-06-25
      • 2012-05-15
      • 2022-11-16
      • 2016-11-13
      • 2022-12-26
      • 1970-01-01
      相关资源
      最近更新 更多