I am trying to run the below code, but eventually not able to run it in Jupyter notebook.
from IPython.display import display
from sklearn import datasets
import data as data
import numpy as nm
import pandas as pd
import matplotlib.pyplot as plt
import pandasql as ps
from pandasql import *
from sqlalchemy import text
from sqlalchemy import sql
from sqlalchemy import create_engine
path = 'D:/train.csv'
titanic = pd.read_csv(open(path), sep=',')
print(titanic.head())
print(titanic.shape)
print (type(titanic))
globals()['titanic']
pysqldf = lambda q: sqldf(q, globals())
query = 'SELECT * FROM titanic LIMIT 3'
pysqldf(query)
The above code I wrote to query a Pandas dataframe using pandasql library features, but it's giving me the below error, not able to figure out what I am missing.
Log:
AttributeError Traceback (most recent call last)
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\sqlalchemy\engine\base.py:1410, in Connection.execute(self, statement, parameters, execution_options)
1409 try:
1410 meth = statement._execute_on_connection
1411 except AttributeError as err:
AttributeError: 'str' object has no attribute '_execute_on_connection'
The above exception was the direct cause of the following exception:
ObjectNotExecutableError Traceback (most recent call last)
Cell In[41], line 2
1 query = 'SELECT * FROM titanic LIMIT 3'
2 pysqldf(query)
Cell In[38], line 1, in <lambda>(q)
1 pysqldf = lambda q: sqldf(q, globals())
Object Not Executable Error: Not an executable object: 'SELECT * FROM titanic LIMIT 3'
Please help.