How to get the first column of a pandas DataFrame as a Series?

497    Asked by Yashraj in Python , Asked on May 28, 2021
x=pandas.DataFrame(...)
s = x.take([0], axis=1)

And s gets a DataFrame, not a Series.

Answered by Ruth Lambert

This will get first column of dataframe pandas as a series:

 import pandas as pd

 df = pd.DataFrame({'x' : [1, 2, 3, 4], 'y' : [4, 5, 6, 7]})
 df
   x y
0 1 4
1 2 5
2 3 6
3 4 7
 s = df.ix[:,0]
 type(s)


Your Answer

Interviews

Parent Categories