Mar-07-2017, 02:38 PM
Dear Python Experts,
I am looking again at Daniel Breen's case study In [69]: he writes a function
called convert_housing_data_to_quarters().
http://danielbreen.net/projects/housing_...ege_towns/
The data is providing columns for every months from 1996-04 to 2016q3.
He is looping through the data to only consider columns from 2000q1 through 2016q3 and
sums them up in quarters. He does that in a nexted loop that
looks like it is dealing specifically with q3 and q4 of 2016.
without a nested loop.
It would be really great if someone has a suggestion how to do that.
Maybe there is a function I dont know yet.
I am looking again at Daniel Breen's case study In [69]: he writes a function
called convert_housing_data_to_quarters().
http://danielbreen.net/projects/housing_...ege_towns/
The data is providing columns for every months from 1996-04 to 2016q3.
He is looping through the data to only consider columns from 2000q1 through 2016q3 and
sums them up in quarters. He does that in a nexted loop that
looks like it is dealing specifically with q3 and q4 of 2016.
for year in range(2000,2017): for quarter in range(1,5): if quarter == 4 and year == 2016: break new_column_name = '{0}q{1}'.format(year, quarter) begin_month = (quarter-1)*3 + 1 end_month = quarter*3 begin_column = '{0}-{1:02d}'.format(year,begin_month) end_column = '{0}-{1:02d}'.format(year,end_month) if quarter == 3 and year == 2016: new_column_name = '2016q3' begin_month = 6 end_month = 8 begin_column = '{0}-{1:02d}'.format(year,begin_month) end_column = '{0}-{1:02d}'.format(year,end_month) data = housing_df.loc[:,begin_column:end_column] housing_df[new_column_name] = data.mean(axis = 1)I wonder if there is an easier way. I would rahter fix the non-existing q3 and q4 problem
without a nested loop.
It would be really great if someone has a suggestion how to do that.
Maybe there is a function I dont know yet.