I have a pandas dataframe as follows:-
import pandas as pd
import numpy as np
from datetime import datetime
start = datetime(2011, 1, 1)
end = datetime(2012, 1, 1)
index = pd.date_range(start, end)
Cols = ['Returns']
df = pd.DataFrame(abs(np.random.randn(366,1)), index=index, columns=Cols)
I need to transform it in such a way that the index is year and columns are months. The expected output is as follows:-
start1 = 2011
end1 = 2012
index1 = (start, end)
cols2=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
df_out = pd.DataFrame(abs(np.random.randn(2,12)), index=index1, columns=cols2)
The single value per year can be sum or average. I tried dataframe groupby as follows:-
DFList = []
for group in df.groupby(df.index.month):
DFList.append(group[1])
r2 = pd.concat([DFList[0], DFList[1] ,DFList[2], DFList[3], DFList[4],
DFList[5],DFList[6],DFList[7],DFList[8], DFList[9],
DFList[10],DFList[11]],ignore_index=True,axis=1)
cols2=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
r2.columns=cols2
I am confused at this point and unable to proceed further. Thanking you in anticipation. Please suggest a way forward.