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.
asked Jan 7, 2021 at 21:41
user14961967
1 Answer 1
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
answered Jan 7, 2021 at 21:50
Paul Brennan
2,7364 gold badges23 silver badges27 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
default
df.head(3)