0

I am getting table from oracle database and converted that into pandas DataFrame (df).I access this dataframe into my summary function and create a new dataframe (crit_df) from it. Till here everything works fine but when I use (crit_df) to create new dataframe(new_df) then I get error as output : no such table: crit_df

Also when I try to print crit_df outside the function then it throws error as : NameError: crit_df is not defined.

So I believe crit_df is a local variable only.

P.S: When I perform all these operation without any function like, creating new_df from crit_df outside any function then everything works fine. But I need summary function to pass numerous criteria. I am using jupyter notebook for this.

Thanks in advance.

import pandas as pd
from pandasql import sqldf

run_qry = lambda q: sqldf(q, globals())
df = run_qry("""select * from **table**""")#getting this **table** from oracle database successfully

def summary(criteria):
 crit_df=run_qry("""select a,b,c from df where amount in """ +criteria+""" group by a""")
 print(crit_df)
 #crit_df is printed till here but cannot move to next line and throws error.
 new_df=  run_qry("""select distinct b from crit_df""")
 #error occurs here

summary("('P')")

OUTPUT: #it prints crit_df dataframe
        OperationalError : no such table crit_df
2
  • globals() will return something different within a function. Have you checked, wether its result is as desired? Commented Feb 10, 2022 at 15:01
  • Hi guidot, Yes, as you mentioned, when I print crit_df then it prints the desired result only. Thanks :) Commented Feb 10, 2022 at 15:14

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.