python - 用数据框中的数组替换 NaN 值
问题描述
我试图用数组 [NaN, NaN, NaN] 替换数据框中的所有 NaN 值。但是正常的替换方法(df.replace)不起作用,因为 to_replace 和 value 的大小不同。
提前致谢 :)
解决方案
我想出了一些东西,应该可以:
import pandas as pd
import numpy as np
# Sample df with a NaN in column "y"
df = pd.DataFrame({"y": np.NaN, "x": ["Some Value"]})
# Get the nans
isna = df['y'].isna()
# Replace with series with list of (3) nans
df.loc[isna, 'y'] = pd.Series([[np.NaN, np.NaN, np.NaN]] * isna.sum()).values
print(df)
推荐阅读
- google-chrome-extension - 有没有办法在韩国销售 Chrome 扩展程序?
- maven - 在主服务项目中构建包括另一个项目的 WebContent 的战争
- spring - 我正在将 spring 与 jsf 集成,并且出现以下异常
- mysql - sql 转储不在 docker-compose 中运行
- jquery - 无论设备分辨率如何,div 的一致行为
- macos - 使用 Arduino 运行 NodeMCU
- microsoft-graph-api - 是否可以通过 MS Graph API 实现“文字即服务”?
- git - Cannot pull with git
- javascript - 从字符串中提取信息 - JavaScript
- c++ - 如何在数组中搜索先前已输入的 int 值