将DataFrame中现有列的值更改为单个特定值

huangapple go评论74阅读模式
英文:

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'

huangapple
  • 本文由 发表于 2020年1月3日 21:23:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579377.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定