I have a dataframe, df, that has values of two characters over quarters of the year. [Q1 = Jan, Feb, Mar; Q2 = Apr, May, Jun, ...etc].
The quarters are the previous four quarters trailing the current quarter (ex. if Current Quarter = Q3 17, quarters measured are Q3 16, Q4 16, Q1 17, Q2 17).
Quarter Q1 17 Q2 17 Q3 16 Q4 16
Character
Sonic 10.0 6.0 19.0 3.0
Shadow 5.0 5.0 7.0 23.0
I would like to reorder the columns in a time applied manner, Q3 16, Q4 16, Q1 17, Q2 17, to get this:
Quarter Q3 16 Q4 16 Q1 17 Q2 17
Character
Sonic 19.0 3.0 10.0 6.0
Shadow 7.0 23.0 5.0 5.0
I've thought about sorting by substring where the year is contained. But is there a cleaner and quicker way to do this?
[EDIT]
For clarification, I created substrings of the year:
columns = list(scar_pivot.columns.values)
sort_columns_years = [int(c[3:5]) for c in columns]
sort_columns_years=
[17, 17, 16, 16]
Since this code in question is used periodically over time, a user would be measuring different quarters depending on when the code is used. (ex. Current quarter is Q2 17, there would be three values of 16 and one value of 17 in sort_columns_years, for Q2-4 16, and Q1 17.
After sorting sort_columns_years, I am thinking I can use count to measure how many of a smaller value exists, to do something in the case of hree quarters in the preceding year like this:
new_list = []
`if sort_columns_years.count(16) == 3:
sort_quarters = ['Q2','Q3','Q4','Q1']
for each in range(0,len(sort_quarters)):
new_list.append(sort_column_years[each] + sort_column_years(sort_quarters[each]))
to get
new_list = ['Q2 16', 'Q3 16', 'Q4 16', 'Q1 17']
but I don't know how to do something similar avoiding hard coding 16 or 17.