I have a dataframe like this:
df = pd.DataFrame({
'Client':['A','B','C','D','E'],
'Revenue':[100,120,50,40,30],
'FYoQ':['FY','Q','Q','Q','FY'],
'Quarter':[np.nan,1,3,4,np.nan],
'Year':[2017,2016,2015,2017,2016]
})
How do I split the data frame to get a 2 dimensional dictionary dataframe
ds[year][quarter] for each year and quarter.
Right now I am able to do a 1 dimensional dictionary as follows:
years=df['Year'].unique().tolist()
mc={elem:pd.DataFrame for elem in years}
for year in years:
mc[year]=df.loc[(df['Year']==year)]
This way I obtain a dictionary of dataframe mc[2015], mc[2016] etc.
And then I again have to apply the same thing to each of them.
I was hoping there would be a modification of the code:
mc={elem:pd.DataFrame for elem in years}
to create a 2 dimensional (or even multi dimensional dictionary) at once, allowing for the splitting of data faster.