我load sklearn里自带的iris数据集,可是读完后它是numpy array的形式,也没有列名什么的,怎么把它转成有列名的pandas.DataFrame的形式?
谢谢各位!
2个回答
方法一:
from sklearn.datasets import load_iris
import pandas as pd
import numpy as np
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
方法二:
iris数据在UCI网站上也有,可以从网站上导入
import pandas
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
dataset = pandas.read_csv(url, names=names) #读取csv数据
谢谢博观大牛!!!
-
ggg818
2018-02-23 22:07
如果你需要把y列也放进dataframe里的话
from sklearn import datasets
import pandas as pd
import numpy as np
iris = datasets.load_iris()
col_names = iris['feature_names'] + ['target']
data = pd.DataFrame(data= np.c_[iris['data'], iris['target']], columns=col_names)