gpt4 book ai didi

python - 值错误: could not convert string to float: '1,141'

转载 作者:行者123 更新时间:2023-12-01 08:20:19 28 4
gpt4 key购买 nike

我正在从 csv 读取数据以执行特征消除。数据如下所示

shift_id    user_id status  organization_id location_id department_id   open_positions  city    zip role_id specialty_id    latitude    longitude   years_of_experience
0 2 9 S 1 1 19 1 brooklyn 48001.0 2.0 9.0 42.643 -82.583 NaN
1 6 60 S 12 19 20 1 test 68410.0 3.0 7.0 40.608 -95.856 NaN
2 9 61 S 12 19 20 1 new york 48001.0 1.0 7.0 42.643 -82.583 NaN
3 10 60 S 12 19 20 1 test 68410.0 3.0 7.0 40.608 -95.856 NaN
4 21 3 S 1 1 19 1 pune 48001.0 1.0 2.0 46.753 -89.584 0.0

这是我的代码 -

dataset = pd.read_csv("data.csv",header = 0)
data = pd.read_csv("data.csv",header = 1)
target = dataset.location_id
#dataset.head()
svm = LinearSVC()
rfe = RFE(svm, 3)
rfe = rfe.fit(data, target)
print(rfe.support_)
print(rfe.ranking_)

但我收到此错误

ValueError: could not convert string to float: '1,141'

我的数据库中没有这样的字符串。

有一些空单元格。所以我尝试使用 -

result.fillna(0, inplace=True)

这导致了这个错误

ValueError: Expected 2D array, got scalar array instead:
array=None.
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

有什么建议如何正确预处理这些数据吗?

这里是示例数据的链接 - https://gist.github.com/karimkhanp/6db4f9f9741a16e46fc294b8e2703dc7

最佳答案

您的 ValueError: could not convert string to float: '1,141' 的解决方案正在使用 thousands你的 pd.read_csv() 中的参数:

dataset = pd.read_csv("data.csv",header = 0, thousands= r",")
dataset.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 14 columns):
shift_id 3 non-null int64
user_id 3 non-null int64
status 3 non-null object
organization_id 3 non-null int64
location_id 3 non-null int64
department_id 3 non-null int64
open_positions 3 non-null int64
city 3 non-null object
zip 3 non-null int64
role_id 3 non-null int64
specialty_id 2 non-null float64
latitude 3 non-null float64
longitude 3 non-null float64
years_of_experience 3 non-null object
dtypes: float64(3), int64(8), object(3)
memory usage: 416.0+ bytes

关于python - 值错误: could not convert string to float: '1,141' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54689666/

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