Let's say that we have the following dataframe. I want to fill the null values of the column height group by column Subject and the following conditions.
- If there is one missing value in a Subject fills the missing value of this Subject with the other value.
- If there are two missing values in a Subject, then fill these missing values by using the median height of all subjects that x == 'AA'
Note: In the desired dataframe we must have the same value per Subject.
df = pd.DataFrame({'Subject': [1,1,2,2,3,3], 'x':['AA','AA','BB','BB','AA','AA'], 'height': [130, np.nan, np.nan, 170, np.nan, np.nan]})
Here is the desired dataframe.

