0

i am very beginner with python and want to import diabetes data from datasets package in order to do the following: 1- see first few records of this array data and see the list of variables and their summary stats 2- convert it into a pandas data frame

for example in the diabetes data set if i want to see the variables names and their type and summary stat before converting them into pandas DF how should i do it? and then how to convert it into pandas DF

from sklearn import datasets
import pandas as pd

# Load the diabetes dataset
diabetes = datasets.load_diabetes()
print(diabetes) 
# convert the diabetes array into pandas
diabetes2 = pd.DataFrame(diabetes)    # DID NOT WORK
datadigger
  • 101
  • 7
  • 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). `df = pd.DataFrame(data=np.c_[diabetes['data'], diabetes['target']], columns=diabetes['feature_names']+['target'])`. – Trenton McKinney May 21 '20 at 17:18
  • It worked however when i tried with boston data which is a regression problem it didnt work – datadigger May 21 '20 at 17:47
  • https://h1ros.github.io/posts/loading-scikit-learns-boston-housing-dataset/ – Trenton McKinney May 21 '20 at 17:54

1 Answers1

0

You can try this:

diabetes_data = pd.DataFrame(diabetes['data'])
diabetes_target = pd.DataFrame(diabetes['target'])
df_c = pd.concat([diabetes_data, diabetes_target], axis=1)
print(df_c)

            0         1         2         3         4         5         6         7         8         9      0
0    0.038076  0.050680  0.061696  0.021872 -0.044223 -0.034821 -0.043401 -0.002592  0.019908 -0.017646  151.0
1   -0.001882 -0.044642 -0.051474 -0.026328 -0.008449 -0.019163  0.074412 -0.039493 -0.068330 -0.092204   75.0
2    0.085299  0.050680  0.044451 -0.005671 -0.045599 -0.034194 -0.032356 -0.002592  0.002864 -0.025930  141.0
3   -0.089063 -0.044642 -0.011595 -0.036656  0.012191  0.024991 -0.036038  0.034309  0.022692 -0.009362  206.0
4    0.005383 -0.044642 -0.036385  0.021872  0.003935  0.015596  0.008142 -0.002592 -0.031991 -0.046641  135.0
..        ...       ...       ...       ...       ...       ...       ...       ...       ...       ...    ...
437  0.041708  0.050680  0.019662  0.059744 -0.005697 -0.002566 -0.028674 -0.002592  0.031193  0.007207  178.0
438 -0.005515  0.050680 -0.015906 -0.067642  0.049341  0.079165 -0.028674  0.034309 -0.018118  0.044485  104.0
439  0.041708  0.050680 -0.015906  0.017282 -0.037344 -0.013840 -0.024993 -0.011080 -0.046879  0.015491  132.0
440 -0.045472 -0.044642  0.039062  0.001215  0.016318  0.015283 -0.028674  0.026560  0.044528 -0.025930  220.0
441 -0.045472 -0.044642 -0.073030 -0.081414  0.083740  0.027809  0.173816 -0.039493 -0.004220  0.003064   57.0

To see all the keys in the diabetes data:

print(diabetes.keys()) 

dict_keys(['data', 'target', 'DESCR', 'feature_names', 'data_filename', 'target_filename'])
NYC Coder
  • 7,424
  • 2
  • 11
  • 24