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
CodePudding user response:
.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')
CodePudding user response:
You can also use:
cond = ~df['verstatus'].isin(['Developed', np.nan])
df['Comments'] = df['Comments'].mask(cond, df['verstatus'] ' Status')
OUTPUT
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 Reserve Status
3 gg.xx.com XX.XXX.XXX.XX Testing Testing Status