I want to replace NA values with the mean of other column with the same year.
Note:
To replace NA values for Canada data, I want to use only the mean of Canada, not the mean from the whole dataset of course.
Here's a sample dataframe filled with random numbers. And some NA how i find them in my dataframe:
| Country | Inhabitants | Year | Area | Cats | Dogs |
|---|---|---|---|---|---|
| Canada | 38 000 000 | 2021 | 4 | 32 | 21 |
| Canada | 37 000 000 | 2020 | 4 | NA | 21 |
| Canada | 36 000 000 | 2019 | 3 | 32 | 21 |
| Canada | NA | 2018 | 2 | 32 | 21 |
| Canada | 34 000 000 | 2017 | NA | 32 | 21 |
| Canada | 35 000 000 | 2016 | 3 | 32 | NA |
| Brazil | 212 000 000 | 2021 | 5 | 32 | 21 |
| Brazil | 211 000 000 | 2020 | 4 | NA | 21 |
| Brazil | 210 000 000 | 2019 | NA | 32 | 21 |
| Brazil | 209 000 000 | 2018 | 4 | 32 | 21 |
| Brazil | NA | 2017 | 2 | 32 | 21 |
| Brazil | 207 000 000 | 2016 | 4 | 32 | NA |
What's the easiest way with pandas to replace those NA with the mean values of the other years? And is it possible to write a code for which it is possible to go through every NA and replace them (Inhabitants, Area, Cats, Dogs at once)?