I got this US police shootings dataset where many
age are null. Each person's
gender is also given.
Here is a sample below:
df[['age', 'race', 'gender']].sample(10) >>> age race gender id 705 27.0 B M 657 23.0 W M 2307 34.0 W M 7529 34.0 NaN M 5871 28.0 H M 1449 37.0 B M 7243 82.0 NaN M 1374 27.0 B M 479 36.0 W M 345 58.0 W M
Instead of simply using the mean or other method such as,
bfill, to fill the null ages, I want to put the average age of the gender of that particular race to which the person belongs. For example, if the average age of males in Hispanic (H) race is 27, I want to put 27 in every null age where the person is a Hispanic Male.
I can't come up with any implementation of this using pandas. I could only proceed as far as this
RAG_df = df[['race', 'gender', 'age']] age_by_race_gender = RAG_df.groupby(['race','gender']).mean() age_by_race_gender >>> age race gender A F 43.67 M 36.04 B F 34.03 M 32.80 H F 32.47 M 33.65 N F 31.20 M 32.62 O F 25.00 M 33.14 W F 39.81 M 40.08
I can't understand how can I get the average age of a race by gender out of here to fill the null values.