【发布时间】:2020-12-15 04:07:12
【问题描述】:
def function(s):
if (s['col1'] == 'something1')|(s['col1'] == 'smth2')|(s['col1'] == 'smth3'):
return 'A'
elif (s['col1'] == 'smth4')|(s['col1'] == 'smth5'):
return 'B'
elif (s['col1'] == 'smth6')|(s['col1'] == 'smth7'):
return 'C'
else:
return 'D'
上面的功能有效。但是当我将它应用到数据框时:
df['new_col'] = df.apply(function, axis = 1)
我明白了:
TypeError: ("'bool' object is not callable", 'occurred at index 0')
【问题讨论】:
-
您确定没有另一个名为
function的变量覆盖了实际的函数定义吗? -
对我来说工作正常。
-
不,没有一个叫做函数的变量。
标签: python pandas function dataframe typeerror