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

前端 未结 6 1506
花落未央
花落未央 2020-12-12 11:36

I tried:

x=pandas.DataFrame(...)
s = x.take([0], axis=1)

And s gets a DataFrame, not a Series.

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-12 12:20

    Isn't this the simplest way?

    By column name:

    In [20]: df = pd.DataFrame({'x' : [1, 2, 3, 4], 'y' : [4, 5, 6, 7]})
    In [21]: df
    Out[21]:
        x   y
    0   1   4
    1   2   5
    2   3   6
    3   4   7
    
    In [23]: df.x
    Out[23]:
    0    1
    1    2
    2    3
    3    4
    Name: x, dtype: int64
    
    In [24]: type(df.x)
    Out[24]:
    pandas.core.series.Series
    

提交回复
热议问题