I would need to assign boolean values to rows in a new column Y based on the value of a column called X (1,2,3,4,5).
I have this column in a dataset df:
X
1
1
1
3
2
5
2
4
1
I would like a new one, Y, in a new dataset that is a copy of df, where:
- if row has X value = 1 then True
- if row has X value = 2 then False
- if row has X value = 3 then False
- if row has X value = 4 then True
- if row has X value = 5 then False
So I should have
X Y
1 true
1 true
1 true
3 false
2 false
5 false
2 false
4 true
1 true
I wrote this code:
new_df=df.copy()
new_df['Y'] = False
for index in df.iterrows():
if df['X'] == 1:
new_df.iloc[index,9] = True
elif df['X'] == 2:
new_df.iloc[index,9] = False
elif df['X'] == 3:
new_df.iloc[index,9] = False
elif df['X'] == 4:
new_df.iloc[index,9] = True
else:
new_df.iloc[index,9] = False
getting this error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Can you please help me to fix the code to get the expected output? Thank you