Having a dataframe consisting of a person and the order...
person order elements
Alice [drink, snack, salad, fish, dessert] 5
Tom [drink, snack] 2
John [drink, snack, soup, chicken] 4
Mila [drink, snack, soup] 3
I want to known what customers had as a main meal. Thus, I want to add another column [main_meal] so that would be my df.
person order elements main_meal
Alice [drink, snack, salad, fish, dessert] 5 fish
Tom [drink, snack] 2 none
John [drink, snack, soup, chicken] 4 chicken
Mila [drink, snack, soup] 3 none
The rule is that if a customer ordered 4 or more meals, it means the 4th element is always the main dish, so I want to extract the 4th element from the list on the order column. If it contains less than 4 elements, then assign 'main_meal' to none. My code:
df['main_meal'] = ''
if df['elements'] >= 4:
df['main_meal'] = df.order[3]
else:
df['main_meal'] = 'none'
It doesn't work:
ValueError Traceback (most recent call last)
<ipython-input-100-39b7809cc669> in <module>()
1 df['main_meal'] = ''
2 df.head(5)
----> 3 if df['elements'] >= 4:
4 df['main_meal'] = df.order[3]
5 else:
~\Anaconda\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1571 raise ValueError("The truth value of a {0} is ambiguous. "
1572 "Use a.empty, a.bool(), a.item(), a.any() or
a.all()."
-> 1573 .format(self.__class__.__name__))
1574
1575 __bool__ = __nonzero__
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
What is wrong with my code?