Boolean indexing pandas

python

#1

Hello I’m a pandas beginner.
I just want to compare values of a Serie with other values. This is an example

df1 = pd.DataFrame({‘A’: [1,0,1,1,1,0,1,0]})
if df1[“A”] == 0:
df1[“test”] = 1
else:
df1[“test”] = 0

The error is “The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().”
Please help me :disappointed_relieved::disappointed_relieved:


#2
df1["test"]=0
for i in range(len(df1)):
  if df1["A"][i] == 0:
    df1["test"][i] = 1
  else:
    df1["test"][i] = 0

@jefferson77 this will work


#3

Yes.
It need a loop to take serie’s values one by one. Logic !
Thank you @pjoshi15