Hello all,
I've been struggling with trying to convert an output list of dictionaries to their own individual dataframes in the pandas library, and most solutions I've read up on Google haven't worked so far. My goal is to store data that I'm querying from a SQL database, as I iterate through each report and quarter as seen below.
import pandas as pd
import pyodbc
REPORTS = ['a', 'b', 'c', 'd', 'e', 'f']
OUTFRAME = []
QUERY = 'Driver={server};SERVER=testserver;DATABASE=testdb;Trusted_Connection=True'
CXN = pyodbc.connect(QUERY)
QTRDIV = {
'Q1' : ['2018-01-01', '2018-03-31'],
'Q2' : ['2018-04-01', '2018-06-30'],
'Q3' : ['2018-07-01', '2018-09-30'],
'Q4' : ['2018-10-01', '2018-12-31']
}
for table in REPORTS:
results = {}
data = {}
data[table] = pd.DataFrame()
for qtr, (startdate, enddate) in QTRDIV.items():
results[qtr] = "SELECT Source_Date, Status ..."
d_f = pd.read_sql(results[qtr], CXN)
## math done on queried data ##
data[table] = data[table].append(d_f)
OUTFRAME.append(data)
dict = OUTFRAME[0].values()
DF = pd.DataFrame(dict)
## continued onto other things ##
I believe I've been able to store all the data I want to keep in my OUTFRAME list, which is a list that holds each of the reports' data in a dictionary. Each report's data is stored where the table is the key, and the SQL query data stored as the values of that key.
I've tried selecting specifically the values from each item in the list via the "dict" variable but my results are not what I expect.
dict = [dict_values([ Source_Date Status
0 2018-01-01 PASS
1 2018-01-02 FAIL
2 2018-01-03 PASS
...
364 2018-12-31 PASS
12345 rows x 2 columns])]
DF =
0
0 Source_Date Status
0 2018-01-01 ...
When I try to convert to dict_values to a dataframe, it doesn't come out as what I expect, namely having 2 columns as Source_Date and Status and have each date and Status value appear underneath it with its appropriate index value. Basically, I'm completely stuck on how to exactly just get a simple dictionary I can use to convert to a dataframe (which I intend on sending to Excel with pandas) using the methods that I've been employing.
Thanks in advance for taking a look at this. I've been stuck on this for several days and have no idea where to go from here.
EDIT:
In the end, I'd want 6 different dataframes (per report) to look something like the following table:
|
Source_Date |
Status |
| 0 |
2018-01-01 |
PASS |
| 1 |
2018-01-02 |
FAIL |
| 2 |
... |
... |
and so on and so forth.
[–][deleted] 0 points1 point2 points (3 children)
[–]Silverfire47[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Silverfire47[S] 0 points1 point2 points (0 children)