【发布时间】:2020-03-30 05:33:56
【问题描述】:
在 Python 3 pandas 数据框中,
a,b
100000,NaN
100000,NaN
100000,NaN
100000,500
10000,5000
我想根据以下逻辑生成一个新的C列:
def applyFunc(a,b):
if a >= 25000 & b is not null:
return b*0.3
elif a >= 25000 & b is null:
return a*0.3
else:
return 0
注意 a & b 列是浮点数,但可以为空。
理想输出:
a,b,c
100000,NaN,30000
100000,NaN,30000
100000,NaN,30000
100000,50000,15000
10000,5000,0
我尝试了以下方法:
df['c']=df.apply(lambda x:applyFunc(df['a'],df['b']), axis=1)
错误:
TypeError: ('cannot compare a dtyped [float64] array with a scalar of type [bool]', 'occurred at index 0')
有什么想法吗?谢谢!
【问题讨论】:
标签: python-3.x pandas lambda apply