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

前端 未结 6 1512
花落未央
花落未央 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:24

    This works great when you want to load a series from a csv file

    x = pd.read_csv('x.csv', index_col=False, names=['x'],header=None).iloc[:,0]
    print(type(x))
    print(x.head(10))
    
    
    
    0    110.96
    1    119.40
    2    135.89
    3    152.32
    4    192.91
    5    177.20
    6    181.16
    7    177.30
    8    200.13
    9    235.41
    Name: x, dtype: float64
    

提交回复
热议问题