Suppose I have 3 SQL dataset tables with the same columns but different values. Those tables contain Tommy's test scores in different subjects on their respective dates,i.e. the table names are '20200601', '20200602' and '20200603'.
Let's say I am interested in extracting Tommy's math test score on 3 different dates by using these 3 tables, so my SQL query would be 'Select subject, test_score from [table name] where subject = 'math'
Suppose the 3 table names are now contained in a list ['20200601', '20200602','20200603'] and as you can see, I can make a for loop to execute the same SQL query based on each table in the list.
I expect to have 3 results in dataframe like (index might be '0' and not date in the result):
From table '20200601':
index subject test_score
0 math 90
From table '20200602':
index subject test_score
0 math 99
From table '20200603':
index subject test_score
0 math 89
So now, ideally I want to combine them into one dataframe which will look like:
From table '20200601':
index subject test_score
0 math 90
1 math 99
2 math 89
And then, I want to merge it into another existing dataframe which is already indexed in date (but have no other columns):
index
20200601
20200602
20200603
After merging the two, it should look like:
From table '20200601':
index subject test_score
20200601 math 90
20200602 math 99
20200603 math 89
I am pretty new to Python, and I know the part of running SQL query in Python (by using pd.read_sql_query) but I am confused about what to do with the look and combine the results together as the next step. Would appreciate your help.
Let's say I am interested in extracting Tommy's math test score on 3 different dates by using these 3 tables, so my SQL query would be 'Select subject, test_score from [table name] where subject = 'math'
Suppose the 3 table names are now contained in a list ['20200601', '20200602','20200603'] and as you can see, I can make a for loop to execute the same SQL query based on each table in the list.
I expect to have 3 results in dataframe like (index might be '0' and not date in the result):
From table '20200601':
index subject test_score
0 math 90
From table '20200602':
index subject test_score
0 math 99
From table '20200603':
index subject test_score
0 math 89
So now, ideally I want to combine them into one dataframe which will look like:
From table '20200601':
index subject test_score
0 math 90
1 math 99
2 math 89
And then, I want to merge it into another existing dataframe which is already indexed in date (but have no other columns):
index
20200601
20200602
20200603
After merging the two, it should look like:
From table '20200601':
index subject test_score
20200601 math 90
20200602 math 99
20200603 math 89
I am pretty new to Python, and I know the part of running SQL query in Python (by using pd.read_sql_query) but I am confused about what to do with the look and combine the results together as the next step. Would appreciate your help.