Yes, Hello coders
I'm tring to assign value to the variable within the data frame based on another variable my data looks like:
Housing_ID Member_ID My_new_staus
1 1
1 2
1 3
1 4
1 5
2 1
2 2
3 1
3 2
3 3
what i want to assign is where the housing id equals to 1 (which is repeated) put the My_new_staus: "Valid"
I tried to apply it through this code:
for i in range (len(df['Housing_ID'])):
if df['Housing_ID'][i] == 1 :
df['My_new_staus'][i] = 'Valid'
else:
df['My_new_staus'][i] = ''
and got this message : # Similar to Index.get_value, but we do not fall back to positional KeyError: 398
the output that i want is
Housing_ID Member_ID My_new_staus
1 1 Valid
1 2 Valid
1 3 Valid
1 4 Valid
1 5 Valid
2 1
2 2
3 1
3 2
3 3
CodePudding user response:
You can use np.where
to assign values to 'My_new_status'
:
df['My_new_status'] = np.where(df['Housing_ID']==1,'valid','')
Output:
Housing_ID Member_ID My_new_status
0 1 1 valid
1 1 2 valid
2 1 3 valid
3 1 4 valid
4 1 5 valid
5 2 1
6 2 2
7 3 1
8 3 2
9 3 3
CodePudding user response:
In general, you should avoid iterating through dataframes. You can just use an .apply()
:
df = \
df.assign(My_new_status = df.Member_ID.apply(lambda row: 'Valid' if x==1 else ''))