I have a Pandas Dataframe like this
| Age | Gender | City |
|---|---|---|
| 10000 | Male | Tokyo |
| 15000 | Male | Tokyo |
| 20000 | Male | Tokyo |
| 12000 | Female | Madrid |
| 14000 | Female | Madrid |
| 16000 | Female | Madrid |
| 15000 | Female | Rome |
| NaN | Female | Rome |
| NaN | Male | Tokyo |
| NaN | Female | Rome |
Those 3 last rows I'd like to input the median based on the gender and city. For example, for the Female in Rome that has NaN value, it would be 15000 because of the only one female of Rome that has 15000.
For the male with Nan values and from Tokyo, it would be 15000 because it is the median of the male of Tokyo.
I know I can fill with the median of the column df['Age'] = df['Age'].fillna(median), but I want to calculate it using the other categorial columns too.
Maybe something like this?
df['Age'] = df['Age].finnla(df[['Age','Gender','City']].groupby(by=['Gender','City']).median())
How can I do this?
Appreciate ur help