如何将单列Pandas DataFrame转换为Series [英] How to convert a single column Pandas DataFrame into Series

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

问题描述

我有以下数据框:

import pandas as pd
d = {'gene' : ['foo','bar'],'score' : [4., 3.,]}
df = pd.DataFrame(d)
df.set_index('gene',inplace=True)

哪个品牌:

In [56]: df
Out[56]:
      score
gene
foo       4
bar       3
In [58]: type(df)
Out[58]: pandas.core.frame.DataFrame

我想要做的是将其变成系列. 我希望它会返回:

What I want to do is to turn it into a Series. I expect it to to return:

gene
foo       4
bar       3
#pandas.core.series.Series

我尝试了这个,但是不起作用:

I tried this but it doesn't work:

In [64]: type(df.iloc[0:,])
Out[64]: pandas.core.frame.DataFrame

In [65]: df.iloc[0:,]
Out[65]:
      score
gene
foo       4
bar       3

什么是正确的方法?

推荐答案

尝试交换括号中的索引:

Try swapping the indices in the brackets:

df.iloc[:,0]

这应该有效.

这篇关于如何将单列Pandas DataFrame转换为Series的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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