I want to impute some blank values with the median for my dataframe which looks like this :
ID Salary Position
1 10 VP
2 VP
3 5 VP
4 15 AVP
5 20 AVP
6 AVP
Now the blank salaries have to be replaced by the position level Median. For example : the blank salary for ID = 2 and position as VP should be imputed by the median of position VP which is 5 and the same blank for AVP should be imputed in a similar fashion.
I have used the following code but this is taking the full median and not the specific one at Position level :
impute_median=df['Salary'].median()
df['Salary']=df['Salary'].fillna(impute_median)
The output should look like this :
ID Salary Position
1 10 VP
2 5 VP
3 5 VP
4 15 AVP
5 20 AVP
6 15 AVP