python - 数据框将现有列值更改为单个特定值

标签 python python-3.x pandas dataframe

有一个带有值的 df

df

0      1
sun   NaN
moon  123
cat   NaN
dog   yatch

将已经存在的值转为通过,将 NaN 转为失败

预期输出

0      1
sun   fail
moon  pass
cat   fail
dog   pass

最佳答案

使用numpy.whereSeries.isna :

df[1] = np.where(df[1].isna(), 'fail', 'pass')

关于python - 数据框将现有列值更改为单个特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59579377/

相关文章:

python - 带有 Python 2 和 Python3 内核的 Jupyter notebook

python - ValueError : Output tensors to a Model must be the output of a TensorFlow Layer with tf. keras Lambda层

python - 为什么我要在 pandas 中复制数据框

python - 在 MVC 中实例化 View 、模型和数据库

python - 尝试将 cvtColor 与 cv2.COLOR_YUV2BGR_Y422 一起使用时出错 - 错误:函数 cv::cvtColor 中的 (-215) scn == 2 && depth == 0

python - 将列分成两部分,同时保留 pandas 中的分隔符

python - 哈希冲突的预期数量

python - 根据组编号将数组数组排序

python - 多维数据集切片和重新索引

python - 在 Pandas 数据框中保留大小为 1 >= k 的 block