Skip to content
Advertisement

A code to find out the count of females and males in a column using python

I want an output in a table as shown below:

 GENDER    SUM
   Male    432
 Female    521

Advertisement

Answer

Example inspired by this article

import pandas as pd

# create a sample dataframe
data = {
    'A': ['E1', 'E2', 'E3', 'E4', 'E5'],
    'B': ['Male', 'Female', 'Female', 'Male', 'Male'],
    'C': [27, 24, 29, 24, 25],
    'D': ['Accounting', 'Sales', 'Accounting', 'Coding', 'Sales']
}
df = pd.DataFrame(data)

print(df['Gender'].value_counts()) # This is what you will use

This prints

Male      3
Female    2
Name: Gender, dtype: int64

You can read more about value_counts() here.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement