gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 09:58:12 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com