'How to create a new column in python based on multiple conditions in a for loop?
I have a dataframe as woe like the below
Variable | Cutoff
0 ABS_Returns | (-88.001, 2.0]
1 ABS_Returns | (2.0, 6.0]
2 ABS_Returns | (6.0, 18.0]
0 AUM | (-0.001, 10.0]
1 AUM | (10.0, 28.26]
0 Amount | (20000, 35638.28]
1 Amount | (35638.28, 48292.0]
and so on..
I need to create a new column in my X dataframe with few conditions based on the woe dataframe. If the condition is True then X['new'] needs to be updated with the index of the woe for each variable.
I am trying to achieve that in a for loop:
for i in woe['Variable'].unique() :
for j in woe[woe['Variable'] == i.index:
condition = [(X[i] >= woe[woe['Variable'] == i][Cutoff].apply(lambda x: x.left)[i]) & (X[i] >= woe[woe['Variable'] == i][Cutoff].apply(lambda x: x.right)[i])
if condition == True:
value = i
else:
pass
X['new'] = np.select(condition, value)
All I am trying to do is if the condition is true then new column should be updated with the index if the condition is not met, it should move on to the 2nd loop. Whenever the condition is true the new column should be updated. By doing this i am creating bins for each range of cutoff's for each variable. But my code throws an error as invalid syntax
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
