How to remplace NaN with conditions on a dataframe


I have this dataframe, I want to replace the NaN of the region column according to the area of membership …

df = pd.DataFrame({
        'area':['North America','Belgique','France','Angleterre','Asie','N Zelande','Italie'],

area           | region |
North America  | NA     |
Belgique       | Nan    |
France         | Europe |
Angleterre     | Autres |
Asie           | Nan    |
N Zelande      | Nan    |
Italie         | Nan    |

i want this output

area           | region |
North America  | NA     |
Belgique       | Europe |
France         | Europe |
Angleterre     | Europe |
Asie           | Autres |
N Zelande      | Autres |
Italie         | Europe |
Asked By: David



Here it is:

dict_of_areas={'NA':'North america','Belgique':'Europe'}

You can use dict_of_areas to map regions into areas.

Answered By: OnY

This you can try.It is filtering the region values where the value is either Nan or NA and assigning the area value to that particular region


For me it’s giving output as below.

area region
North America North America
Belgique Belgique
France Europe
Angleterre Autres
Asie Asie
N Zelande N Zelande
Italie Italie

Is this the same output you are expecting?

Answered By: Akshay MG