I have a df (shape (5928, 22)) and I'm trying to create a new column and add values based on multiple conditions.
The conditions would be:
if CH == 20 then value = 268,34
if CH == 24 then value = 322,02
if CH == 30 then value = 492,65
if CH == 40 then value = 536,69
and
if CH == 20 & ID in (5105561300, 5105561301, 5105561302, 5105561304) then value = 417,43
if CH == 24 & ID in (5105561300, 5105561301, 5105561302, 5105561304) then value = 500,91
if CH == 30 & ID in (5105561300, 5105561301, 5105561302, 5105561304) then value = 626,34
if CH == 40 & ID in (5105561300, 5105561301, 5105561302, 5105561304) then value = 834,85
When I try to add a new column and append the values based on the first block of conditions it works perfectly.
new_value = []
for row in df['CH']:
if row == 20:
new_value.append(268.34)
elif row == 24:
new_value.append(322.02)
elif row == 30:
new_value.append(402.65)
elif row == 40:
new_value.append(536.69)
else:
new_value.append(0)
df['new_value'] = new_value
When I tried to add other conditions it wont work. Code would be something like:
new_value = []
for row in df['CH']:
if row == 20 and df['ID'] not in ((5105561300, 5105561301, 5105561302, 5105561304):
new_value.append(268.34)
elif row == 20 and df['ID'] in ((5105561300, 5105561301, 5105561302, 5105561304):
new_value.append(417.43)
elif row == 24 and df['ID'] not in ((5105561300, 5105561301, 5105561302, 5105561304):
new_value.append(268.34)
elif row == 24 and df['ID'] in ((5105561300, 5105561301, 5105561302, 5105561304):
new_value.append(500.91)
elif row == 30 and df['ID'] not in ((5105561300, 5105561301, 5105561302, 5105561304):
new_value.append(268.34)
elif row == 30 and df['ID'] in ((5105561300, 5105561301, 5105561302, 5105561304):
new_value.append(626.34)
elif row == 40 and df['ID'] not in ((5105561300, 5105561301, 5105561302, 5105561304):
new_value.append(268.34)
elif row == 40 and df['ID'] in ((5105561300, 5105561301, 5105561302, 5105561304):
new_value.append(834.85)
else:
new_value.append(0)
df['new_value'] = new_value
When I try the code above I get the following error message:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I don't know how to go from here. In SQL would two simple WHERE but I cannot get it to work in python.
I would appreciate any help!
Aucun commentaire:
Enregistrer un commentaire