Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 234
SHIVAM GOYAL Avatar asked Oct 21 '25 11:10

SHIVAM GOYAL


2 Answers

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()
like image 108
jezrael Avatar answered Oct 23 '25 00:10

jezrael


There's a special method for that - pd.Series.to_frame

In [2]: df = pd.DataFrame({'a': range(4)})

In [3]: df.a
Out[3]: 
0    0
1    1
2    2
3    3
Name: a, dtype: int64

In [4]: df.a.to_frame()
Out[4]: 
   a
0  0
1  1
2  2
3  3
like image 44
Ami Tavory Avatar answered Oct 22 '25 23:10

Ami Tavory



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!