使用pandas dataframe.query()选择列 [英] Select columns using pandas dataframe.query()

查看:542
本文介绍了使用pandas dataframe.query()选择列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dataframe.query()上的文档非常 http://pandas.pydata.org/pandas-docs/stable/generation/pandas.DataFrame.query.html .我也无法通过网络搜索找到投影的例子.

The documentation on dataframe.query() is very terse http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.query.html . I was also unable to find examples of projections by web search.

因此,我尝试仅提供列名:这给出了语法错误.对于键入select,然后键入列名称也是如此.那么..该怎么做?

So I tried simply providing the column names: that gave a syntax error. Likewise for typing select and then the column names. So .. how to do this?

推荐答案

玩了一段时间之后,通读了

After playing around with this for a while and reading through the source code for DataFrame.query, I can't figure out a way to do it.

如果不是不可能的话,显然强烈建议不要这样做.当这个问题出现在github上时,多产的Pandas dev/maintainer jreback 建议使用df.eval()用于选择列,df.query()用于对行进行过滤.

If it's not impossible, apparently it's at least strongly discouraged. When this question came up on github, prolific Pandas dev/maintainer jreback suggested using df.eval() for selecting columns and df.query() for filtering on rows.

更新:

javadba指出eval的返回值不是数据帧.例如,充实jreback的示例……

javadba points out that the return value of eval is not a dataframe. For example, to flesh out jreback's example a bit more...

df.eval('A')

返回熊猫系列,但是

df.eval(['A', 'B'])

不返回DataFrame,而是返回(熊猫系列的)列表.

does not return at DataFrame, it returns a list (of Pandas Series).

因此,看来保持灵活性以对行和列进行过滤的最佳方法最终是使用iloc/loc,例如

So it seems ultimately the best way to maintain flexibility to filter on rows and columns is to use iloc/loc, e.g.

df.loc[0:4, ['A', 'C']]

输出

          A         C
0 -0.497163 -0.046484
1  1.331614  0.741711
2  1.046903 -2.511548
3  0.314644 -0.526187
4 -0.061883 -0.615978

这篇关于使用pandas dataframe.query()选择列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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