SHIVAM GOYAL SHIVAM GOYAL - 3 months ago 24
Python Question

convert Series to DataFrame

I created a dataframe 'x'

I wanted to create another dataframe y which consists of values of feature 'wheat_type' from dataframe x

so i executed the code

y=X.loc[:, 'wheat_type']


when I ran the following command

y['wheat_type'] = y.wheat_type("category").cat.codes


I got following error


'Series' object has no attribute 'wheat_type'


on executing type(X),I got

<class 'pandas.core.frame.DataFrame'>


and on executing type(y),I got

<class 'pandas.core.series.Series'>


Is there possible way to covert y into a dataframe.If not,please tell me how to create required dataframe y from x

Answer

It looks like need astype and to_frame:

X = pd.DataFrame({'wheat_type':[5,7,3]})
print (X)
   wheat_type
0           5
1           7
2           3

#create DataFrame by subset
y=X[['wheat_type']]

#cast to category and get codes
y['wheat_type'] = y.wheat_type.astype("category").cat.codes
print (y)
   wheat_type
0           1
1           2
2           0

If there are multiple columns, better is use to_frame as pointed Ami:

X = pd.DataFrame({'wheat_type':[5,7,3], 'z':[4,7,9]})
print (X)
   wheat_type  z
0           5  4
1           7  7
2           3  9

y = X['wheat_type'].to_frame()

#cast to category and get codes
y['wheat_type'] = y.wheat_type.astype("category").cat.codes
print (y)
   wheat_type
0           1
1           2
2           0

Another solution for creating new DataFrame is by subset and copy:

y = X[['wheat_type']].copy()