0

I am trying to convert this Python code section to pandas dataframe:

iris = datasets.load_iris()
x = iris.data
y = iris.target

I will like to import Iris data on my local machine instead of loading the data from Scikit library. Your kind suggestions would be highly appreciated.

Dean
  • 129
  • 2
  • 9
  • 2
    Does this answer your question? [How to convert a Scikit-learn dataset to a Pandas dataset?](https://stackoverflow.com/questions/38105539/how-to-convert-a-scikit-learn-dataset-to-a-pandas-dataset). Also, see https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html – JAV Oct 30 '20 at 00:09
  • Not really, my problem requires the steps in these links reversed. I have tried them all out with no success thus far. Thanks! – Dean Oct 30 '20 at 00:30

1 Answers1

1

Use from_records and concat to create a datafram. then rename the columns:

df = pd.concat([pd.DataFrame.from_records(x),pd.DataFrame(y)],axis=1)
df.columns = ['col1','col2','col3','col4','target']
Mehdi Golzadeh
  • 2,594
  • 1
  • 16
  • 28
  • I need some explanations on your kind answer. Thanks – Dean Oct 30 '20 at 00:32
  • 1
    I created a data frame from x np array and a data frame from y array. then using pandas. concat function contacted them together with columns (axis=1). – Mehdi Golzadeh Oct 30 '20 at 00:33
  • your code worked perfectly for Scikit to pd dataframe but I am trying to move from pd dataframe to Scikit! Your kind suggestions will be appreciated. – Dean Oct 30 '20 at 12:04
  • 1
    Simply select the X = df.iloc[:,:n-1] y = df[['target']] And you can use train_test_split to split to train and test sets – Mehdi Golzadeh Oct 30 '20 at 14:14