df.loc[(df['Platform'] == 'A321') and (df['Days'] <= 14), 'Mods Applicable'] = 'CFM56 9th Stage Duct'
我正在尝试使A321和Days的Platforms的输出小于或等于14,以打印出以下“适用的Mods”,但并没有说Platforms and the Days。
输出结果:
IF和输出:
答案 0 :(得分:1)
尝试使用“&”代替“ and”:
df = pd.DataFrame({'Platform': ['A123', 'A321', 'A321', 'B123'],
'Days': [10, 13, 20, 5]})
df.loc[(df['Platform'] == 'A321') & (df['Days'] <= 14), 'Mods Applicable'] = 'CFM56 9th Stage Duct'
print(df)
Platform Days Mods Applicable
0 A123 10 NaN
1 A321 13 CFM56 9th Stage Duct
2 A321 20 NaN
3 B123 5 NaN
我可以通过使用“和”而不是“&”来重现相同的错误:
Traceback (most recent call last):
File "<ipython-input-128-7957a219684b>", line 1, in <module>
df.loc[(df['Platform'] == 'A321') and (df['Days'] <= 14), 'Mods Applicable'] = 'CFM56 9th Stage Duct'
File "/Users/nathanielgates/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py", line 1479, in __nonzero__
.format(self.__class__.__name__))
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
答案 1 :(得分:0)
这是因为将Series
与值进行比较也会返回Series
。这意味着(df['Platform'] == 'A321')
和(df['Days'] <= 14)
的结果是Series
值的true/false
,默认情况下不会解析为单个true或false。所以你不能and
!
您可以将and
替换为&
。它将变成:
df.loc[(df['Platform'] == 'A321') & (df['Days'] <= 14), 'Mods Applicable'] = 'CFM56 9th Stage Duct'