英文:
Dataframe change existing columns values to a single particular value
问题
将已经存在的值更改为"pass",将NaN更改为"fail"。
期望输出:
0 1
sun fail
moon pass
cat fail
dog pass
英文:
have a df with values
df
0 1
sun NaN
moon 123
cat NaN
dog yatch
Turn the values that are already present to pass and NaN to fail
expected Output
0 1
sun fail
moon pass
cat fail
dog pass
答案1
得分: 5
Use numpy.where
with Series.isna
:
df[1] = np.where(df[1].isna(), 'fail', 'pass')
英文:
Use numpy.where
with Series.isna
:
df[1] = np.where(df[1].isna(), 'fail', 'pass')
答案2
得分: 0
A variation without numpy
:
mask = df[1].isna()
df.loc[mask, 1] = 'fail'
df.loc[~mask, 1] = 'pass'
英文:
A variation without numpy
:
mask = df[1].isna()
df.loc[mask, 1] = 'fail'
df.loc[~mask,1] = 'pass'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论