小编典典

如何将 pandas DataFrame 的第一列作为一个系列?

all

我试过了:

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

s获得一个 DataFrame,而不是一个系列。


阅读 120

收藏
2022-08-27

共1个答案

小编典典

>>> 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)
<class 'pandas.core.series.Series'>
>>>

====================================================

更新

如果您在 2017 年 6 月之后阅读此内容,ix则已在 pandas 0.20.2
中弃用,因此请不要使用它。使用lociloc代替。请参阅此问题的评论和其他答案。

2022-08-27