Yar*_*riv 126 python series dataframe pandas
我试过了:
x=pandas.DataFrame(...)
s = x.take([0], axis=1)
Run Code Online (Sandbox Code Playgroud)
并s
获得一个DataFrame,而不是一个系列.
her*_*rfz 130
>>> 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'>
>>>
Run Code Online (Sandbox Code Playgroud)
================================================== =========================
UPDATE
如果您在2017年6月之后阅读此内容,ix
已在pandas 0.20.2中弃用,请不要使用它.使用loc
或iloc
代替.请参阅此问题的评论和其他答案.
HYR*_*YRY 105
您可以通过以下代码将第一列作为系列获取:
x[x.columns[0]]
Run Code Online (Sandbox Code Playgroud)
Jef*_*eff 85
从v0.11 +,...使用df.iloc
.
In [7]: df.iloc[:,0]
Out[7]:
0 1
1 2
2 3
3 4
Name: x, dtype: int64
Run Code Online (Sandbox Code Playgroud)
小智 13
这不是最简单的方法吗?
按列名称:
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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
261290 次 |
最近记录: |