gpt4 book ai didi

python - Pandas:获取具有列值的行并用另一个值更新注释列

转载 作者:行者123 更新时间:2023-12-05 02:35:59 25 4
gpt4 key购买 nike

我有下面的数据框

       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

所以当verstatus不等于“Developed”或“NaN”(其他任何东西)时我需要更新然后评论

所以预期的结果是

       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

我试过了

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)

但它不能正常工作

最佳答案

.notna() 是一个 pandas 函数,但是 x['version status'] 是一个字符串,你应该使用 pd.isna 像这样:

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)

我认为这样会更干净:

# 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')

关于python - Pandas:获取具有列值的行并用另一个值更新注释列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70373757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com