I have the following data and I want to create a new column with certain conditions. See the following:
DataSets:
real,rel
1,0
0,1
1,1
0,1
0,0
0,0
1,1
1,1
0,0
0,1
1,0
1,1
0,1
1,0
The code I tried and the error I received:
>>> import pandas as pd
>>> df = pd.read_csv("test.csv")
>>> df.loc[df["real"]==0 and df["rel"]==0,"out"] = 9
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python35\lib\site-packages\pandas\core\generic.py", line 1576, 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().
I have the condition for the out
column as:
when real
is 0
and rel
is 0
, out
should be 0
when real
is 1
and rel
is 1
, out
should be 1
when real
is 1
and rel
is 0
, out
should be 2
when real
is 0
and rel
is 1
, out
should be 3
Please let me know what I can do to fulfill the missing part.
I have checked this: Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()