简体   繁体   English

如何将熊猫数据帧转换为numpy数据帧

[英]how to convert pandas data frame into numpy data frame

I have one simple data set with class label and stored as "mydata.csv", 我有一个带有类标签的简单数据集,并存储为“ mydata.csv”,

GA_ID   PN_ID   PC_ID   MBP_ID  GR_ID   AP_ID   class
0.033   6.652   6.681   0.194   0.874   3.177     0
0.034   9.039   6.224   0.194   1.137   3.177     0
0.035   10.936  10.304  1.015   0.911   4.9       1
0.022   10.11   9.603   1.374   0.848   4.566     1

i simply use given code to convert this data into numpy array so that i can use this data set for predictions and machine learning modeling but due to header is error has been raised "ValueError: could not convert string to float: " when i removed header from the file this method work well for me : 我只是使用给定的代码将此数据转换为numpy数组,以便我可以将此数据集用于预测和机器学习建模,但是由于标头出现错误,出现了“ ValueError:无法将字符串转换为float:”,当我移除标头时从文件中此方法对我来说效果很好:

import numpy as np
#from sklearn import metrics
#from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC

raw_data = open("/home/me/Desktop/scklearn/data.csv")
dataset = np.loadtxt(raw_data, delimiter=",")
X = dataset[:,0:5]
y = dataset[:,6]

i also tried to skip header but error occurs: 我也试图跳过标题,但发生错误:

dataset = np.loadtxt(raw_data, delimiter=",")[1:]

then i moved to pandas and able import data from this method: 然后我搬到了熊猫,并能够通过这种方法导入数据:

raw_data = pandas.read_csv("/home/me/Desktop/scklearn/data.csv")

but here I sucked again when i tried to convert this into numpy array its showing error like previous. 但是在这里,当我尝试将其转换为numpy数组时,如前所述,它再次显示错误。

is there any method available in pandas that can : save heathers as list : 熊猫有没有可用的方法可以:将石南花另存为列表:

header_list = ('GA_ID','PN_ID','PC_ID' ,'MBP_ID' ,'GR_ID' , 'AP_ID','class')

last column as class label and remaining part(1:4,0:5) to numpy array for model building: 最后一列作为类标签,剩余部分(1:4,0:5)到numpy数组用于模型构建:

I have write down a code to get column list 我已经写下代码以获取列列表

clm_list = []
raw_data = pandas.read_csv("/home/me/Desktop/scklearn/data.csv")
clms = raw_data.columns()
for clm in clms:
    clm_list.append(clm)
print clm_list ## produces column list

after reading a lot finally I achieved what I want and successfully implemented data on scikit-learn, code to convert CSV data with scikit-learn compatible form is given bellow. 经过大量阅读后,终于达到了我想要的目标,并在scikit-learn上成功实现了数据,下面给出了使用scikit-learn兼容形式转换CSV数据的代码。 thanks 谢谢

import pandas as pd
r = pd.read_csv("/home/zebrafish/Desktop/ex.csv")
print r.values

clm_list = []
for column in r.columns:
    clm_list.append(column)


X = r[clm_list[0:len(clm_list)-1]].values
y = r[clm_list[len(clm_list)-1]].values

print clm_list
print X
print y

out come of this code is exactly what i want : 这段代码出来的正是我想要的:

['GA_ID', 'PN_ID', 'PC_ID', 'MBP_ID', 'GR_ID', 'AP_ID', 'class']

[[  0.033   6.652   6.681   0.194   0.874   3.177]
 [  0.034   9.039   6.224   0.194   1.137   3.177]
 [  0.035  10.936  10.304   1.015   0.911   4.9  ]
 [  0.022  10.11    9.603   1.374   0.848   4.566]]

[0 0 1 1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM