【问题标题】:Pandas transform with custom functionPandas 使用自定义函数进行变换
【发布时间】:2016-12-19 22:46:07
【问题描述】:

这是我的简单代码

def custom_func(x):
    for el in x.index:
        print(el)
    return None

df = pd.DataFrame({'A':['F','F','F','K','K'], 'B':[54,87,35,25,82],
                       'C':[56,78,0,14,13]})

mask = df.groupby(['A'])['B'].transform(custom_func)

它会生成以下错误消息:

Traceback (most recent call last):
  File "C:/Users/temp3.py", line 59, in <module>
    consecutive_check()
  File "C:/Users/temp3.py", line 56, in consecutive_check
    mask = df.groupby(['A'])['B'].transform(custom_func)
  File "C:\Anaconda3\lib\site-packages\pandas\core\groupby.py", line 2763, in transform
    result[indexer] = res
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

怎么了,为什么我会收到这个错误信息。

【问题讨论】:

  • 尝试替换None --> np.nan

标签: python pandas transform


【解决方案1】:

也许是因为你的函数总是返回None,不管输入是什么。您能否更完整地描述您期望的最终结果?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 2023-01-02
    • 2015-07-04
    • 2017-05-03
    • 2014-01-28
    相关资源
    最近更新 更多