I am trying to impute missing values using the most frequent value by a group using the pandas module in Python. After checking some posts on Stack Overflow, I have managed to achieve that:
import numpy as np
import pandas as pd
df = pd.DataFrame({"group": ["A", "A", "A", "A", "B", "B", "B"],
"value": [1, 1, 1, np.nan, 2, np.nan, np.nan]})
df.groupby("group").transform(lambda x: x.fillna(x.mode().iloc[0]))
Running this code will fill with 1 the missing entry for group "A" and with 2 both missing entries for group "B". However, let's assume that one of the groups contains only missing data (in this case group "B"):
df1 = pd.DataFrame({"group": ["A", "A", "A", "A", "B", "B", "B"],
"value": [1, 1, 1, np.nan, np.nan, np.nan, np.nan]})
df1.groupby("group").transform(lambda x: x.fillna(x.mode().iloc[0]))
Running the code above will prompt an IndexError: single positional indexer is out-of-bounds. I would expect the normal behaviour is to keep np.nan because if you run the method mode for just, let's say, group "B" from df1:
df1[df1.group == "B"].mode()
I will get that NaN is the most common value. How can I avoid this issue?