Python Pandas:将选定的列保留为DataFrame而不是Series [英] Python pandas: Keep selected column as DataFrame instead of Series

查看:423
本文介绍了Python Pandas:将选定的列保留为DataFrame而不是Series的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当从pandas DataFrame中选择单个列时(例如df.iloc[:, 0]df['A']df.A等),结果矢量将自动转换为Series而不是单列DataFrame.但是,我正在编写一些将DataFrame作为输入参数的函数.因此,我更喜欢处理单列DataFrame而不是Series,以便该函数可以假定df.columns是可访问的.现在,我必须使用pd.DataFrame(df.iloc[:, 0])之类的方法将Series显式转换为DataFrame.这似乎不是最干净的方法.是否有更优雅的方法直接从DataFrame进行索引,以使结果为单列DataFrame而不是Series?

When selecting a single column from a pandas DataFrame(say df.iloc[:, 0], df['A'], or df.A, etc), the resulting vector is automatically converted to a Series instead of a single-column DataFrame. However, I am writing some functions that takes a DataFrame as an input argument. Therefore, I prefer to deal with single-column DataFrame instead of Series so that the function can assume say df.columns is accessible. Right now I have to explicitly convert the Series into a DataFrame by using something like pd.DataFrame(df.iloc[:, 0]). This doesn't seem like the most clean method. Is there a more elegant way to index from a DataFrame directly so that the result is a single-column DataFrame instead of Series?

推荐答案

正如@Jeff提到的,有几种方法可以做到这一点,但是我建议使用loc/iloc来使其更加明确(如果您尝试某些操作,请提早出错)模棱两可):

As @Jeff mentions there are a few ways to do this, but I recommend using loc/iloc to be more explicit (and raise errors early if your trying something ambiguous):

In [10]: df = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 'B'])

In [11]: df
Out[11]:
   A  B
0  1  2
1  3  4

In [12]: df[['A']]

In [13]: df[[0]]

In [14]: df.loc[:, ['A']]

In [15]: df.iloc[:, [0]]

Out[12-15]:  # they all return the same thing:
   A
0  1
1  3

在整数列名称的情况下(这正是创建loc/iloc的原因),后两种选择消除了歧义.例如:

The latter two choices remove ambiguity in the case of integer column names (precisely why loc/iloc were created). For example:

In [16]: df = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 0])

In [17]: df
Out[17]:
   A  0
0  1  2
1  3  4

In [18]: df[[0]]  # ambiguous
Out[18]:
   A
0  1
1  3

这篇关于Python Pandas:将选定的列保留为DataFrame而不是Series的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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