The pandas dataframe is:
df = pd.DataFrame([['A', 1, 20], ['A', 2, 30], ['B', 1, 50], ['B', 3, 45], ['A', 4, 60], ['B', 5, 70]])
df.columns = ['Type', 'P', 'X']
df:
Type P X
0 A 1 20
1 A 2 30
2 B 1 50
3 B 3 45
4 A 4 60
5 B 5 70
Expectation:
I want to apply nested conditions to calculate a value and append it as a new column.
This is what I did:
result = 0
if (df.Type == 'A'):
if df.P % 2 == 0:
result = df.X 10
else:
restult = df.X 20
else:
if df.P % 2 == 0:
result = df.X 30
else:
result = df.X 40
df['Result'] = result
The expected output is:
Type P X Result
0 A 1 20 40
1 A 2 30 40
2 B 1 50 90
3 B 3 45 85
4 A 4 60 70
5 B 5 70 110
But it failed with error: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I've dug other threads but they seem to be other issues.
Any suggestion is appreciated
CodePudding user response:
Use numpy's np.where(if condition, result if condition is met, alternative if condition is not met)
df['result']=np.where((df.Type == 'A')&(df.P % 2 == 0),df.X 10,df.X 30)
Type P X result
0 A 1 20 50
1 A 2 30 40
2 B 1 50 80
3 B 3 45 75
4 A 4 60 70
5 B 5 70 100
CodePudding user response:
You have to iterate over the rows to calculate the result
result = []
for i, row in df.iterrows():
if (row.Type == 'A'):
if row.P % 2 == 0:
result.append(row['X'] 10)
else:
result.append(row['X'] 30)
else:
if row.P % 2 == 0:
result.append(row['X'] 30)
else:
result.append(row['X'] 10)
# and then add the column
df['Result'] = result