My aim is to put values in column D based on columns A to C. I want to go through each of the columns A, B and C and add 1 to column D if the value is greater than 20.
import pandas as pd
data={'A':[5,2,25,4],"B":[15,22,100,24], "C":[4, 100, 0, 19], "D" : [0,0,0,0]}
df= pd.DataFrame(data)
for x in range(0, len(df.columns)):
if df.iloc[:, x] > 20:
df["D"] = df["D"] +1
else:
df["D"] = df["D"]
df
So, based on the sample code, Column D should looks like:
D |
---|
0 |
2 |
2 |
1 |
However, I keep getting this error - ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). I have gone through other answers here (Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()) which indicate that it is a multiple operator issue but I am not using these here.
What am I doing wrong?