值错误:无法将字符串转换为浮点数。用 Pandas 打开 CSV 文件

我正在尝试打开一个用于决策树学习的 CSV 数据集。当我运行代码时,结果出现值错误。我认为问题出在逗号上,但我不知道如何处理它。


import pandas as pd

from sklearn.tree import DecisionTreeClassifier 

from sklearn.model_selection import train_test_split 

from sklearn import metrics 


col_names = ['pregnant', 'glucose', 'bp', 'skin', 'insulin', 'bmi', 'pedigree', 'age', 'label']


pima = pd.read_csv(r'D:\MachinLearning\MyDataSets_Implementations\pima-indians-diabetes.csv', header=None, names=col_names)


pima.head()

一些数据集行如下:


    Pregnancies,Glucose,BloodPressure,SkinThickness,Insulin,BMI,DiabetesPedigreeFunction,Age,Outcome

6,148,72,35,0,33.6,0.627,50,1 1,85,66,29,0,26.6,0.351,31,0

8,183,64,0,0,23.3,0.672,32,1 1,89,66,23,94,28.1,0.167,21,0

0,137,40,35,168,43.1,2.288,33,1 5,116,74,0,0,25.6,0.201,30,0


30秒到达战场
浏览 85回答 1
1回答

天涯尽头无女友

我根本没有收到错误。在pd.csv_read(... sep=","). 您还应该添加(..., skiprows=1)该函数,以不将文件头作为第一个数据行读取。import pandas as pdcol_names = ['pregnant', 'glucose', 'bp', 'skin', 'insulin', 'bmi', 'pedigree', 'age', 'label']csv_path = r'D:\MachinLearning\MyDataSets_Implementations\pima-indians-diabetes.csv'pima = pd.read_csv(csv_path, header=1, names=col_names, sep=",", skiprows=1)print(pima.head())给出输出pregnant  glucose  bp  ...  pedigree  age  label 6 148 72 35 0   33.6 0.627 50      1 1       85  66  ...     0.351   31      08 183 64 0  0   23.3 0.672 32      1 1       89  66  ...     0.167   21      00 137 40 35 168 43.1 2.288 33      1 5      116  74  ...     0.201   30      0[3 rows x 9 columns]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python