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

标签 python pandas scikit-learn

我正在从 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/

相关文章:

Python 套接字 I/O 性能与其他语言的比较

python - 透视 Pandas Dataframe,没有数字类型,索引不是唯一的

python-3.x - 使用 Pandas 从数据框中获取同名行列表

python - 不可散列类型 : 'dict' while applying a function with pandas?

python - 按所有键的唯一性对字典列表进行排序

python - 如何在Python中使用RSA私钥(不是普通签名)加密数据?

python - 如何解释 : Label Ranking Average Precision Score

python - 使用 Python 对特定数据集列表进行交叉验证

python - Scikit-learn 是否发布了 python GIL?

python - 计算矩阵元素的加权和的最快方法是什么?