Below is the data that I have:
There are three columns the class, date and marks
I need my output to be in the format below: Here column headers 1,2,3 are the classes which contain the total marks stored month and year wise
My approach to this was using the following logic: (Although I get the result, I am not satisfied with the code. I was hoping that someone could help me with a more efficient solution for the same?)
class=sorted(df.Class.unique())
dict_all = dict.fromkeys(class , 1)
for c in class:
actuals=[]
for i in range(2018,2019):
for j in range(1,13):
a = df['date'].map(lambda x : x.year == i)
b = df['date'].map(lambda x : x.month == j)
x= df[a & b & (df.class==c)].marks.sum()
actuals.append(x)
dict_all[c]=actuals
result = pd.DataFrame.from_dict(dict_all)