In Python (using pandas and numpy) I have a CSV list (example.csv) and I am looking to find the average age of the males in my data set. How would I make a function ('avg_age_males') that finds and outputs the average age for just the males in the data set? (In the .csv males is represented by 'M' and females is represented by 'F')
1) Importing libraries
import pandas as pd
import numpy as np
2) Reading the .csv file
df = pd.read_csv("example.csv")
print(df)
3) Getting the male gender data
male = df[df.gender == 'm']
print(male)
4) Getting the age from the male dataframe
male_age = male['age']
print(male_age)
5) Calculating the mean
male_age.mean()
We can combine the points of 3, 4, and 5 into one single line
df[df.gender == 'm']['age'].mean()
I hope this answered your question. If you have any further doubts do let me know in the comment. I will incorporate the doubt in the answer. Regards.
Get Answers For Free
Most questions answered within 1 hours.