I have a dataframe that looks something like that below:
col1 col2
0 abc 0
1 def -1
2 ghi 1
3 jkl -0.5
repro:
data = {'col1': ['abc', 'def','ghi','jkl'],
'col2': ['0', '-1','1','-0.5']
}
df = pd.DataFrame (data, columns = ['col1','col2'])
I'd like to add a third column, the contents of whichare based on conditional evaluation of col2 so the result is as follows:
col1 col2 col3
0 abc 0 blue
1 def -1 red
2 ghi 1 green
3 jkl -0.5 red
My current code is this:
df['col3'] = np.where((df['col2'] >=1,'green',
(df['col2'] ==0, 'blue',
(df['col2'] <0, 'red'))))
However, this currently fails with the following error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-151-a19019bc0d01> in <module>
1 df['col3'] = np.where((df['col2'] >=1,'green',
2 (df['col2'] ==0, 'blue',
----> 3 (df['col2'] <0, 'red'))))
//anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in __nonzero__(self)
1476 raise ValueError("The truth value of a {0} is ambiguous. "
1477 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1478 .format(self.__class__.__name__))
1479
1480 __bool__ = __nonzero__
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Could I please ask you to explain the error and suggest how I can achieve me end goal?
Thanks