0

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.

1
  • 1
    I would just use vanilla pandas: df.head(3) Commented Jan 7, 2021 at 22:02

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.