Search code examples
pythonpandasdata-analysis

Count values in one column based on the categories of other column


I'm working on the following dataset:

dataset

and I want to count each value in the LearnCode column for each Age category, I've tried doing it using Groupby method but didn't manage to get it correctly, can anyone help on how to do it?


Solution

  • You can do this using a groupby on two columns

    results = df.groupby(by=['Age', 'LearnCode']).count()
    

    This outputs a count for each ['Age', 'LearnCode'] pair