如何将pandas DataFrame的第一列作为系列? [英] How to get the first column of a pandas DataFrame as a Series?

查看:187
本文介绍了如何将pandas DataFrame的第一列作为系列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过:

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

s获得一个DataFrame,而不是Series.

And s gets a DataFrame, not 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)
<class 'pandas.core.series.Series'>
>>>

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

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

更新

如果您在2017年6月之后阅读此内容,ix在熊猫0.20.2中已被弃用,因此请不要使用它.使用lociloc代替.查看对此问题的评论和其他答案.

If you're reading this after June 2017, ix has been deprecated in pandas 0.20.2, so don't use it. Use loc or iloc instead. See comments and other answers to this question.

这篇关于如何将pandas DataFrame的第一列作为系列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆