I have seen a lot of cases missing values are either filled by mean or medians. I was wondering how can we fill misssing values with frequency.
Here is my setup:
import numpy as np
import pandas as pd
df = pd.DataFrame({'sex': [1,1,1,1,0,0,np.nan,np.nan,np.nan]})
df['sex_fillna'] = df['sex'].fillna(df.sex.mode()[0])
print(df)
sex sex_fillna
0 1.0 1.0 We have 4 males
1 1.0 1.0
2 1.0 1.0
3 1.0 1.0
4 0.0 0.0 we have 2 females, so ratio is 2
5 0.0 0.0
6 NaN 1.0 Here, I want random choice of [1,1,0]
7 NaN 1.0 eg. 1,1,0 or 1,0,1 or 0,1,1 randomly
8 NaN 1.0
Is there a generic way it can be done so?
My attempt
df['sex_fillan2'] = df['sex'].fillna(np.random.randint(0,2)) # here the ratio is not guaranteed to approx 4/2 = 2
NOTE This example is only for binary values, I was looking for categorical values having more than two categories.
For example:
class: A B C
20% 40% 60%
Then instead of filling all nans by class C I would like to fill according to frequency counts.
But, is this a good idea?
As per some comments, this might or might not be a good idea to impute missing values with different values for different rows, I have created a question in CrossValidated, if you want to give some inputs or see if this is a good idea visit the page: https://stats.stackexchange.com/questions/484467/is-it-better-to-fillnans-based-on-frequency-rather-than-all-values-with-mean-or