Reputation: 413
df.loc[(df['Platform'] == 'A321') and (df['Days'] <= 14), 'Mods Applicable'] = 'CFM56 9th Stage Duct'
I'm trying to have the output of Platforms for A321 and Days less than or equal to 14 to print out the following 'Mods Applicable' but it's not ouputting that Platforms AND the Days.
Output results:
IF & Output:
Upvotes: 2
Views: 97
Reputation: 1905
This is because comparing a Series
to a value also returns a Series
. Which means that the results of (df['Platform'] == 'A321')
and (df['Days'] <= 14)
are Series
of true/false
values and it doesn't resolve to a single true or false by default. So you can't and
it!
You can replace the and
with &
. So it would become:
df.loc[(df['Platform'] == 'A321') & (df['Days'] <= 14), 'Mods Applicable'] = 'CFM56 9th Stage Duct'
Upvotes: 0
Reputation: 3290
Try using "&" instead of "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
I can reproduce the same error by using "and" instead of "&":
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().
Upvotes: 1