Imputing missing value based on filtering result of another column C1
C2




A
x


A
y


A
z


A
x


A
NaN


A
x


A
x


A
x


B
y


B
y


B
z


B
y


B
NaN


B
y


B
x


B
x




I have to impute missing values in C2 , the imputation should be such that if the missing values corresponding C1 column is A, then filter the datset by A and find the mode of C2 and replace the missing value, similarly for B.
In the above example it should be x for first missing value and y for second missing value




C1
C2




A
x


A
y


A
z


A
x


A
x


A
x


A
x


A
x


B
y


B
y


B
z


B
y


B
y


B
y


B
x


B
x




Tried this, not sure if this is the best way
for i in df['c1'].unique(): 
  df[(df['c1']==i) &amp; (df['c2'].isnull())]['c2'] = df[df['c1']==i]['c2'].mode()[0]
You can use pandas groupby and transform methods. This maps the groupwise mode to the index of the original dataframe.
df['C2']=df.groupby('C1').transform(lambda x: x.fillna(x.value_counts().index[0]))['C2']
df

The output is: