【发布时间】:2017-09-23 13:00:45
【问题描述】:
我正在尝试使用将两列作为参数的函数在 pandas 数据框中创建一个新列
def ipf_cat(var, con):
if var == "Idiopathic pulmonary fibrosis":
if con in range(95,100):
result = 4
if con in range(70,95):
result = 3
if con in range(50,70):
result = 2
if con in range(0,50):
result = 1
return result
然后
df['ipf_category'] = ipf_cat(df['dx1'], df['dxcon1'])
其中 df['dx1'] 是一列和一个字符串,df['dxcon1'] 是另一列和一个 0-100 的整数。该函数在python中运行良好,但我不断收到此错误
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我看过之前的回答比如
Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
但我无法针对我的特定功能实施这些解决方案。
【问题讨论】: