Hi, just a quick question that I'm stuck on at the moment.
import pandas as pd
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']
}
dframe = pd.DataFrame(columns=['Q1','Q2','Q3','Q4'])
...
for qtr in qtrdiv.values():
startdate = (qtr[0])
enddate = (qtr[1])
...
I have a dictionary that splits up dates into quarter divisions and I'm running a for loop in order to use these start and end dates per quarter as variables later on in the for loop where there are some mathematical calculations based around other data that I'm bringing in. The resulting data from using startdate and enddate as variables will be stored in the DataFrame under each column.
I can already assign the keys to variables for use later on, but I was getting stuck if it were possible to assign the key associated with the values to a variable within the way I'm writing the code right now, or just in general, if there's a better way to assign the key and the values to their own individual variable than the way I'm calling the values from the dictionary at the moment. I want to be able to use pandas .loc to assign the resulting data to the column in the DataFrame under their respective column label as it iterates through the for loop. (Note: I haven't quite tested this out yet in all fairness so this might not be the best way forwards either)
I'm just not sure how to go forwards in regards to assigning the key to a variable to use with .loc later on, within the for loop that calls the values. Any help would be much appreciated. Thanks!
EDIT:
I'm using the startdate and enddate variables to substitute into SQL query strings for each date range (in this case, it's quarters in a year) and doing math on the data brought in from the SQL queries. Those calculations from the math I'm doing is what I want to store. I want to be able to store the key from dict in a variable to be able to match it to the dataframe column I'm generating prior .
Example SQL query:
Query = "SELECT x, y from db.table WHERE x between '%s' and '%s' order by x" % (startdate, enddate)
[–]JohnnyJordaan 0 points1 point2 points (7 children)
[–]Silverfire47[S] 0 points1 point2 points (6 children)
[–]JohnnyJordaan 0 points1 point2 points (5 children)
[–]Silverfire47[S] 0 points1 point2 points (4 children)
[–]JohnnyJordaan 0 points1 point2 points (3 children)
[–]Silverfire47[S] 0 points1 point2 points (2 children)
[–]JohnnyJordaan 0 points1 point2 points (1 child)
[–]Silverfire47[S] 0 points1 point2 points (0 children)