34

I have the following data frame just single column.

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

Currently it has the following shape.

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

How can I convert it to a Series or a numpy vector so that the shape is simply (5,)

neversaint
  • 60,904
  • 137
  • 310
  • 477

1 Answers1

48

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 as numpy array, you can use .values accessor , Example -

In [27]: tdf['s1'].values
Out[27]: array([  0. ,   1. ,  23.4,  10. ,  23. ])
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • 1
    or perhaps assign the series to a numpy array for further manipulation, ie. np.asarray(tdf['s1']) – Anzel Oct 28 '15 at 07:37
  • 3
    Yea, but I think `tdf['s1'].values` would be cleaner. – Anand S Kumar Oct 28 '15 at 07:52
  • This can have the following error http://stackoverflow.com/questions/39474056/valueerror-object-too-deep-for-desired-array – Keith Apr 17 '17 at 22:56
  • 8
    If you wish to avoid reference to the column name, you can alternatively do `tdf.iloc[:,0]` to get the leftmost column, `tdf.iloc[:,1]` for the next one... – Ben Mares Jan 14 '20 at 22:46