How to remplace NaN with conditions on a dataframe

Question:

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'],
        'region':['NA','Nan','Europe','Autres','Nan','Nan','Nan']})`


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

||

Answers:

Here it is:

dict_of_areas={'NA':'North america','Belgique':'Europe'}
df.loc[df['region'].isnull(),'region']=df.loc[df['region'].isnull(),'area'].replace(dict_of_areas)

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

    df['region'][df['region'].isin(["Nan","NA"])]=df['area']

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