在PandaSQL中使用用户输入变量 [英] Using user input variables in PandaSQL
问题描述
我正在尝试在已有的数据帧上使用pandaSQL,我想知道是否存在使用变量的方法,或者是否存在另一种使用变量的方法.我想要做的是将用户输入设置为变量,然后尝试在SQL语句中使用它.我想在输入时显示该形状的每个实例.我正在尝试以下方法:
I'm trying to use pandaSQL on a dataframe that I have and I'm wondering if there's a way to use variables or if there's another way to do it. What I'm trying to do is setting user input as a variable and then trying to use that in the SQL statement. I want to display every instance that a shape when input. I'm trying stuff along the lines of:
variable1 = input("Enter shape here: ")
print pysqldf("SELECT imageNum FROM df WHERE shape1 = variable1 ")
但是到目前为止还没有运气.其他一切工作正常,引入变量时我遇到了麻烦.是否可以在pandaSQL中使用?如果没有,我可以使用哪些解决方法?
but so far no luck. Everything else is working fine, I'm just running into trouble when introducing variables. Is this possible in pandaSQL and if not, what workarounds could I use?
推荐答案
您需要在SQL查询中将WHERE
子句传递给块引用中的参数,即:
You'll need to pass the WHERE
clause in the SQL query an argument in blockquotes, i.e.:
从df中选择imageNum shape1 =正方形".
SELECT imageNum FROM df WHERE shape1 = "square".
因此,您需要传递一个包含引号的字符串.请尝试以下操作:
So you'll need to pass a string that includes the quotation marks. Try the following:
variable1 = input(在此处输入形状:")
variable1 = input("Enter shape here: ")
variable1str ="\""+变量1 +" \"
variable1str = "\"" + variable1 +"\""
print pysqldf(从df WHERE shape1 = variable1str"中选择imageNum"
print pysqldf("SELECT imageNum FROM df WHERE shape1 = variable1str ")
这样,如果您输入的是"square"
:
This way, if your input is, say, "square"
:
打印变量1
`>正方形
print variable1
`> square
打印变量1str
'>正方形"
print variable1str
'> "square"
这篇关于在PandaSQL中使用用户输入变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!