I have a dataset in pandas, with four columns (year, month, day, register).
df_registration
I want to group the data by year and month, and then count for each month in a year how many 'yes' and how many 'no' there were (or at least how many 'yes').
Image of df with expected result:
I have tried doing group_by count,
g = df_registration.groupby(["year", "month"])
monthly_counts = g.aggregate({"register": pd.Series.value_counts })
but the outputs do not give the desired outcome, they just count the number of both register values.
Image of df with failed attempt:
I cannot get it to work as I want...
EDIT!____________ solution _________________
The code from alex smolyakov in the comment works!
counts = df_registration.groupby(["year", "month"]["register"].value_counts()
CodePudding user response:
counts = df_registration.groupby(["year", "month"])["register"].value_counts()
does it help ?