How to convert pandas data frame of one column to a series or a numerical vector

I have the following data frame: only one column.

import pandas as pd tdf = pd.DataFrame({'s1' : [0,1,23.4,10,23]}) 

Currently, it has the following form.

 In [54]: tdf.shape Out[54]: (5, 1) 

How can I convert it to a series vector or numpy, so the form is simple (5,)

+8
python pandas
source share
1 answer

You can simply index the series you want. Example -

 tdf['s1'] 

Demo -

 In [24]: tdf = pd.DataFrame({'s1' : [0,1,23.4,10,23]}) In [25]: tdf['s1'] Out[25]: 0 0.0 1 1.0 2 23.4 3 10.0 4 23.0 Name: s1, dtype: float64 In [26]: tdf['s1'].shape Out[26]: (5,) 

If you want the values ​​in the series to be a numpy array, you can use .values accessor, Example -

 In [27]: tdf['s1'].values Out[27]: array([ 0. , 1. , 23.4, 10. , 23. ]) 
+14
source share

All Articles