user14961967
user14961967

Reputation:

failed to run a simple SQL query for pandas dataframe

I trying to run SQL query on a Pandas dataframe using pandasql.

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.

Upvotes: 0

Views: 319

Answers (1)

Paul Brennan
Paul Brennan

Reputation: 2696

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

Upvotes: 1

Related Questions