Issue
I have the below dataframe
FQDN ip address verstatus Comments
0 pp.xx.com XX.XXX.XXX.XX Developed NaN
1 qq.xx.com XX.XXX.XXX.XX NaN NaN
2 gg.xx.com XX.XXX.XXX.XX Reserve NaN
3 gg.xx.com XX.XXX.XXX.XX Testing NaN
so I need to update then comments when the verstatus not equal "Developed" or "NaN"(else anything)
so expected result would be
FQDN ip address version status Comments
0 pp.xx.com XX.XXX.XXX.XX Developed NaN
1 qq.xx.com XX.XXX.XXX.XX NaN NaN
2 gg.xx.com XX.XXX.XXX.XX Reserve Reserve Status
3 gg.xx.com XX.XXX.XXX.XX Testing Testing Status
I tried
df['Comments'] = df.apply(lambda x: x["version status"]+" Status" if x['version status'] != 'Developed' or x['version status'].notna() else x['Comments'], axis=1)
But it not works properly
Solution
.notna()
is a pandas function, but x['version status']
is a string, you should use pd.isna
like this:
df['Comments'] = df.apply(lambda x: x['Comments'] if x['version status']=='Developed'
or pd.isna(x['version status'])
else x["version status"]+" Status",
axis=1)
I think this would be cleaner:
# version status is either Developed or nan
cond = df['verstatus'].eq('Developed') | df['verstatus'].isna()
# you can update with `np.where`
df['Comments'] = np.where(cond, df['Comments'], df['verstatus'] + ' Status')
Answered By - Quang Hoang
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.