i have a data set which looks like this:
yg = pd.DataFrame({'grade': ['a','a','b','b','a'],
'key2': ['one','two','one','two','one'],
'year': (2012,2013,2012,2012,2013),
'id': (1101,2212,2331,2432,3464)})
which counts the number of users in each year by categories
yg.groupby(['year','grade']).groups.count()
Also, this is my work: I am trying to build a function,
def User_Grades(data,year):
g = data.groupby(['year']).get_group(year).groupby(['grade']).size[['a','b']]
for i in df_groupby(['year']).groups.key():
print('{}\n'.format(i), 'a: {}\n'.format(User_Grades(df,i)['a'],'b: {}\n'.format(User_Grades(df,i)['b'])))
I would like to input the year, so i could have the information of that year, not all years. for example,
User_Grades(yg,['2012'])
# I would have
2012
a : 2
b : 2
Note: I received some advice about using pivot in python. However, the output of pivot is different with the expected answer. There is no ':' in pivot.
Pivot gives below output:
YEAR GRADE
2012 a 2
b 2
2013 a 1
b 0
This format from pivot is not expected, instead I need this:
2012
a : 2
b : 2
2013
a : 1
b : 0