【发布时间】:2019-09-16 11:31:36
【问题描述】:
我有一个如下的数据框,我需要生成一个名为“Comment”的新列,对于指定的值,它应该显示“Fail”
输入:
Tel MC WT
AAA Rubber 9999
BBB Tree 0
CCC Rub 12
AAA Other 20
BBB Same 999
DDD Other-Same 70
尝试过的代码:
df.loc[(df[WT] == 0 | df[WT] == 999 | df[WT] == 9999 | df[WT] == 99999),'Comment'] = 'Fail'
错误:
AttributeError: 'str' object has no attribute 'loc'
预期输出:
Tel MC WT Comment
AAA Rubber 9999 Fail
BBB Tree 0 Fail
CCC Rub 12
AAA Other 20
BBB Same 999 Fail
DDD Other-Same 70
【问题讨论】:
-
错误提示
df是一个字符串,而不是一个数据框对象。检查type(df)
标签: python python-3.x pandas dataframe