Home > Software design >  Aggregate by two columns and count occurrences of distinct values in third column in pandas
Aggregate by two columns and count occurrences of distinct values in third column in pandas

Time:10-07

I have a dataset in pandas, with four columns (year, month, day, register).

df_registration

data table image

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:

desired outcome

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:

wrong

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()

the code output here

CodePudding user response:

counts = df_registration.groupby(["year", "month"])["register"].value_counts()

does it help ?

  • Related