Home > Software design >  How to remplace NaN with conditions on a dataframe
How to remplace NaN with conditions on a dataframe

Time:01-08

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']})

Region

NA = North Americ/

Europe = France, Belgique, Italie, Angleterre/

Autres = N Zelande, Asie/

CodePudding user response:

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?

CodePudding user response:

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.

  •  Tags:  
  • Related