pandas - 我正在尝试将 csv 文件的空白值转换为列的平均值,但它给出 "could not convert string to float: ' -' "错误

标签 pandas numpy machine-learning scikit-learn python-3.6

import pandas as pd 
import numpy as np
from sklearn.preprocessing import Imputer 
data = pd.read_csv("austin_weather.csv")
data = data.drop(['Events', 'Date'], axis = 1) 
X = data.iloc[:, :-1].values
Y = data.iloc[:, 18].values
data = data.replace('T', 0.0)
imputer = Imputer(missing_values="-", strategy="mean", axis = 0)
imputer.fit(X[:])

imputer 函数无法将“-”空白值转换为相应列的平均值

最佳答案

对于折旧类别sklearn.preprocessing.Imputer参数 missing_values 是字符串 NaN 或数字。

因此,您可以首先将所有 - 值替换为 np.nan: X.replace('-', np.nan) 和然后调用imputer。

关于pandas - 我正在尝试将 csv 文件的空白值转换为列的平均值,但它给出 "could not convert string to float: ' -' "错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60136264/

相关文章:

pandas - 我如何使用 scipy optimize curve fit with panda df

python - Pandas 条件过滤器

python - 从 python dataframe 列中删除非 json 对象行

python - 具有给定特征值和特征向量的随机正半定矩阵

machine-learning - 如何使用 Vowpal Wabbit 的上下文强盗学习排名?

android - 如何使用机器学习和神经网络快速构建图像识别应用程序原型(prototype)?

python - Pandas 组 : usage in this instance

python - numpy - 对数组的每一行应用聚合

python - scipy.sparse.csr_matrix.toarray() 的大量内存使用

python - decision_tree_regressor 模型的负 cross_val_score