Search code examples
pythonpandas-groupbydata-sciencefiltering

How to determine gender count in a data grouped with groupby in python programming language?


In the data set of a market, each data belongs to a product group. I want to group this data group by fische number and find the total number of male and female customers. The data set is as in the picture. dataset The number of unique fischeno is 141783. Therefore, the total number of customers should be 141783.

For example


Solution

  • If you just wanna count the total number of each gender column, you may try this:

    df = pd.read_csv('./data.csv')
    df = df.groupby('ficheno').first()
    male_count = df[df.gender == 'm'].gender.count()
    female_count = df[df.gender == 'f'].gender.count()
    

    This will result in male = 2, female = 3 according to your sample dataset.