如何在 Pandas 的查询中使用变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30340277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use variables inside query in Pandas?
提问by user437777
I have problem quering the data frame in panda when I use variable instead of value.
当我使用变量而不是值时,我在查询Pandas中的数据框时遇到问题。
df2 = pd.read_csv('my.csv')
query=df2.query('cc_vehicle_line==7')
works fine but
工作正常,但
df2 = pd.read_csv('my.csv')
query=df2.query('cc_vehicle_line==variable_name')
It throws the message that variable_name is undefined.But it is defined. I cannot use hardcoded value as I need to automate and depending of value of variable_name, select relevant rows.
它抛出变量名未定义的消息。但它已定义。我不能使用硬编码值,因为我需要自动化并根据 variable_name 的值选择相关行。
Am I missing something?
我错过了什么吗?
Thanks
谢谢
回答by Zero
You should use @variable_namewith @
您应该使用@variable_name与@
query=df2.query('cc_vehicle_line==@variable_name')

