简体   繁体   English

如何使用一系列值来估算/替换 Pandas DataFrame 中的缺失值?

[英]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.

相关问题 如何用模式/平均值估算 pandas dataframe 中的整个缺失值? - How to impute entire missing values in pandas dataframe with mode/mean? 如何将默认值归入 Pandas dataframe 列? - How to impute default values to Pandas dataframe columns? 为每个熊猫数据框ID替换一列中序列的缺失值 - Replace missing values of a sequence in a column for each id of pandas dataframe 使用 Pandas dataframe 中的线性回归预测来估算缺失值 - Impute missing values with prediction from linear regression in a Pandas dataframe 如何遍历 pandas 数据框以估算另一个数据框中存在的缺失值? - How do I iterate over a pandas dataframe to impute missing values that are present in another data frame? 如何使用 KNN 估算缺失值 - How to impute missing values with KNN 替换pandas数据帧中的值(不包括缺失值) - replace values in a pandas dataframe (excluding missing values) 如何使用大熊猫估算缺失值? - How Do I impute missing values using pandas? 用于估算缺失值的线性回归 pandas python - Linear regression to impute missing values pandas python 我需要使用 pandas dataframe 根据第二个分类变量中的值来估算分类变量的缺失值 - I need to impute the missing values of a categorical variable based on the values in second categorical variable using pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM