列名称以数字开头时,Pandas查询引发错误 [英] Pandas query throws error when column name starts with a number

查看:129
本文介绍了列名称以数字开头时,Pandas查询引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对以下数据帧执行查询:

I'm trying to perform a query on the following dataframe:

data = {'ab': [1,2,3], 'c1': [1,2,3], 'd': [1,2,3], 'e_f': [1,2,3]}
df = pd.DataFrame(data)
for cl in df.columns:
    print len(df.query('%s==2' %cl))

这很好.但是,如果列名以数字开头,则会引发语法错误.

This works fine. However, if a column name starts with a number then it throws a syntax error.

data = {'ab': [1,2,3], 'c1': [1,2,3], '1d': [1,2,3], 'e_f': [1,2,3]}
df = pd.DataFrame(data)
for cl in df.columns:
    print len(df.query('%s==2' %cl))

文件"的第1行

1 d ==2
  ^

SyntaxError:语法无效

SyntaxError: invalid syntax

我认为问题与字符串的格式有关.我想知道什么是构成此查询的正确方法.

I think that the problem is related to the format of the string. I was wondering what will be the correct way to form this query.

推荐答案

query使用pandas.eval,即1d在Python中不是有效的语法,因此您不能使用query那样引用此列.

query uses pandas.eval, which is documented to "evaluate a Python expression as a string". Your query is not a valid Python expression, because 1d is not valid syntax in Python, so you can't use query to refer to this column that way.

如果您确保所有列都是有效的Python标识符,那么使用pandas进行操作通常会更容易.

Things in pandas are generally easier if you make sure all your columns are valid Python identifiers.

这篇关于列名称以数字开头时,Pandas查询引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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