How to convert a Scikit-learn dataset to a Pandas dataset?

后端 未结 22 2168
清酒与你
清酒与你 2020-11-28 19:10

How do I convert data from a Scikit-learn Bunch object to a Pandas DataFrame?

from sklearn.datasets import load_iris
import pandas as pd
data = load_iris()
p         


        
22条回答
  •  长情又很酷
    2020-11-28 19:28

    There might be a better way but here is what I have done in the past and it works quite well:

    items = data.items()                          #Gets all the data from this Bunch - a huge list
    mydata = pd.DataFrame(items[1][1])            #Gets the Attributes
    mydata[len(mydata.columns)] = items[2][1]     #Adds a column for the Target Variable
    mydata.columns = items[-1][1] + [items[2][0]] #Gets the column names and updates the dataframe
    

    Now mydata will have everything you need - attributes, target variable and columnnames

提交回复
热议问题