How to fill in mode for missing values by group using a for loop in Python?

Viewed 11

I would like to fill in missing values in my data with the mode, and group this by region. I have code that works when I name the variable, but not when I try to run it as a loop.

This code is working for me:

    data['mode'] = data.groupby('REGION')['FAM_TYPE'].transform(lambda x: x.value_counts().idxmax())
    data['FAM_TYPE'].fillna(data['mode'], inplace=True)

But nesting this in a for loop to run over select variables (v) this code returns an error:

    variables = ['VAR1', 'VAR2', ...]
    for v in data[variables]:
        data['mode'] = data.groupby('REGION')[v].transform(lambda x: x.value_counts().idxmax())
        data[v].fillna(data['mode'], inplace=True)
    
    Output: ValueError: attempt to get argmax of an empty sequence

I have something similar that works for median...

This works:

for v in data[numerics]:
    data[v] = data[v].fillna(data.groupby('REGION')[v].transform('median'))
    data[v] = data[v].fillna(data[v].median())

But I can't figure out how to do the same for mode

0 Answers
Related