# How do I fill the null ages of a person according to the average age of males or females in a particular race

I got this US police shootings dataset where many age are null. Each person's race and 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, ffill, 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 groupby() operation:

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.

• How about you don't do this? This will likely bias your results, why do you need this? Dec 21, 2022 at 7:15
• Yeah, I was thinking that it may produce inappropriate data. Still I want to know how to do this from coding knowledge perspective. Dec 21, 2022 at 7:17
• What do you suggest would be preferable to fill the null values? Dec 21, 2022 at 7:21
• I don't know, how you treat missing values is very dependent on what your goal is, what kind of analysis you will run, and what options you have available. Dec 21, 2022 at 8:04

RAG["age2"] = RAG_df.groupby(['race','gender'])["age"].transform("mean")

• Instead of substituting, is there any other way such, as pandas fillna()? I tried with this code: df['new'] = df.groupby(['race','gender'])['age'].transform(lambda x: x.fillna(x.mean())), then with this: df["age"] = df['age'].fillna(df.groupby(['race','gender'])['age'].transform('mean')), but none are working. It's returning the same column as the age column. Dec 23, 2022 at 7:28