There are multiple tables in SQL DB based on countries where I need to extract data from. How can I extract multiple data in one script from multiple tables to use in a dataframe to multiple outputs based on the countries. How can I call this list in the dataframe for the output?
I am merging different results at the end to export the file. Currently its based on one country only (US). I'm trying to combine other countries results in one script to export all together.
country_name = ["US", "Canada"] #trying to extract data from different
SQL tables which is saved in a format target_US_category
query = """SELECT * FROM DB.[Appservices\DBPowerUsers].
[target_""" + country_name +"""_category];"""
df= pd.read_sql(query, engine)
def summary_tables (df):
female = df[df['sex1']=='Female'].shape[0]
male = df[df['sex1']=='Male'].shape[0]
category = pd.DataFrame([
['Male', male, (round(((male/(df.shape[0]))*100),2))],
['Female', female, (round(((female/(df.shape[0]))*100),2))]],
columns = ['category', 'Amt', 'Percent'])
return(category)
def summary_tables (df1):
female = df1[df1['sex1']=='Female'].shape[0]
male = df1[df1['sex1']=='Male'].shape[0]
category1 = pd.DataFrame([
['Male', male, (round(((male/(df1.shape[0]))*100),2))],
['Female', female, (round(((female/(df1.shape[0]))*100),2))]],
columns = ['category', 'Amt', 'Percent'])
return(category1)
df_merge = pd.merge(summary_tables (df),summary_tables
(df1), on='category')