'How to run python sqldf module inside static function?
I have this code which works fine:
import pandas as pd
import sqldf
df1 = pd.DataFrame({'col1': [1, 2, 3], 'col2': ['Jhon1', 'Jhon2', 'Jhon3']})
df2 = pd.DataFrame({'col1': [1, 2, 3], 'col2': ['Doe1', 'Doe2', 'Doe3']})
sql_query = '''
select * from df1
left join df2
on df1.col1=df2.col1
'''
result_df = sqldf.run(sql_query)
result_df
However when I wrap it inside class function like this:
import pandas as pd
import sqldf
class MyAmazingClass:
def static_function():
df1 = pd.DataFrame({'col1': [1, 2, 3], 'col2': ['Jhon1', 'Jhon2', 'Jhon3']})
df2 = pd.DataFrame({'col1': [1, 2, 3], 'col2': ['Doe1', 'Doe2', 'Doe3']})
sql_query = '''
select * from df1
left join df2
on df1.col1=df2.col1
'''
result_df = sqldf.run(sql_query)
return result_df
df = MyAmazingClass.static_function()
df
I am getting this error:
AttributeError: module 'main' has no attribute 'df2'
How to make this code work in class function?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
