[英]How to impute/replace missing values in a pandas DataFrame with a sequence of values?
For example, suppose I have this DataFrame:例如,假设我有这个 DataFrame:
Weights = pd.DataFrame({'Weight': [46, np.nan, 67, 62, np.nan, np.nan, 88, np.nan, 55, np.nan]})
Weights
Weight
0 46.0
1 NaN
2 67.0
3 62.0
4 NaN
5 NaN
6 88.0
7 NaN
8 55.0
9 NaN
And I would like to replace/impute the NaN values with the following sequence of values:我想用以下值序列替换/估算 NaN 值:
replace = np.random.randint(45,90, size=(5,))
replace
array([85, 79, 68, 72, 52])
Such that the resulting DateFrame looks like:这样生成的 DateFrame 看起来像:
Weights
Weight
0 46
1 85
2 67
3 62
4 79
5 68
6 88
7 72
8 55
9 52
What code do I need?我需要什么代码? Could this be done using standard python code, only pandas, or only scikit-learn?这可以使用标准的 Python 代码、仅 Pandas 或仅使用 scikit-learn 来完成吗? Thanks in advance.提前致谢。
我刚刚想通了
weights.replace({np.nan:replace})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.