I have a dataframe of dim (5004346 x 14). I am trying to add a new column (Risk_level) based if values in one column (iter) match the values in different lists.
I have:
def assign_risk(df):
if df['iter'].isin(no):
val = 'no_risk'
elif df['iter'].isin(low):
val = 'low_risk'
elif df['iter'].isin(med):
val = 'med_risk'
else:
val = 'high_risk'
return val
# create new column with risk attribute
df['Risk_level'] = df.apply(assign_risk, axis=1)
df
But i'm getting the error 'str' object has no attribute 'isin'.
A kind of mwe:
temp = pd.DataFrame({'iter' : [1,1,1,2,2,2,3,3,3,4,4,4,4]})
temp2 = [1,2]
temp3 = [3]
temp4 = [4]
def test_temp(df):
if df['iter'].isin(temp2):
val = 'no_risk'
elif df['iter'].isin(temp3):
val = 'low_risk'
else:
val = 'high_risk'
return val
temp['test'] = temp.apply(test_temp, axis=1)
temp
Also returns 'numpy.int64' object has no attribute 'isin'
Why isn't this working?? i've been trying for hours and don't understand....