简体   繁体   English

未能针对 pandas dataframe 运行简单的 SQL 查询

[英]failed to run a simple SQL query for pandas dataframe

I trying to run SQL query on a Pandas dataframe using pandasql.我尝试使用 pandasql 在 Pandas dataframe 上运行 SQL 查询。

import pandas as pd
import pandasql.sqldf as exec_sql

df = pd.DataFrame({'a': [1, 2, 3], 'b':[7, 8, 9]})
exec_sql("select top(3) * from df")

I'm having this error, that I could not find the reason.我有这个错误,我找不到原因。

error message:
PandaSQLException: (sqlite3.OperationalError) near "from": syntax error
[SQL: select top(3) * from df]
(Background on this error at: http://sqlalche.me/e/e3q8)

I try to read carefully the background of the error, from the error's message here but I could not found the cause.我尝试从此处的错误消息中仔细阅读错误的背景,但找不到原因。 Maybe anyone might help to give me a track, please.也许任何人都可以帮助给我一个曲目,拜托。

I trying to run SQL query on a Pandas dataframe using pandasql.我尝试使用 pandasql 对 Pandas dataframe 运行 SQL 查询。

import pandas as pd
import pandasql.sqldf as exec_sql

df = pd.DataFrame({'a': [1, 2, 3], 'b':[7, 8, 9]})
exec_sql("select top(3) * from df")

I'm having this error, that I could not find the reason.我有这个错误,我找不到原因。

error message:
PandaSQLException: (sqlite3.OperationalError) near "from": syntax error
[SQL: select top(3) * from df]
(Background on this error at: http://sqlalche.me/e/e3q8)

I try to read carefully the background of the error, from the error's message here but I could not found the cause.我尝试从此处的错误消息中仔细阅读错误的背景,但找不到原因。 Maybe anyone might help to give me a track, please.也许有人可以帮忙给我一个轨道,拜托。

this is what you want这就是你想要的

import pandas as pd
import pandasql.sqldf as exec_sql

df = pd.DataFrame({'a': [1, 2, 3], 'b':[7, 8, 9]})
exec_sql("select * from df LIMIT 3;")

Limit 3 will give you the first 3 rows限制 3 将为您提供前 3 行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM