Home > OS >  How can I replace nan values from filtered multiple columns on pandas?
How can I replace nan values from filtered multiple columns on pandas?

Time:01-31

I want to clean NaN values at my data frame separately. I used some filters for finding the NaN values. But same filter detected that another column include NaN values. This condition is confused my mind. I tried a lot of methods but these NaN values don't change.

Firstly see my dataframe;

Raw DataFrame

When the applied filters

pop[(pop['Log GDP per capita'].isna())][(pop['Country name'])=='Somalia']

I used a lot of methods, but I can't change anything.

# Method 1
pop[(pop['Log GDP per capita']).isna()]['Log GDP per capita'].fillna(8,inplace=True)
# Method 2
pop['Log GDP per capita'] = pop['Log GDP per capita'].replace(np.nan,8,inplace=True)
# Method 3
pop[(pop['Log GDP per capita'].isna())][(pop['Country name'])=='Somalia']['Log GDP per capita'].replace(np.nan,7.6,inplace=True)
# Method 4
pop[(pop['Log GDP per capita'].isna())][(pop['Country name'])=='Somalia'].mask(pop['Log GDP per capita']=='', 7.946, inplace=True)
# Method 5
pop[(pop['Log GDP per capita'].isna())][(pop['Country name'])=='Somalia'].mask(pop['Log GDP per capita']==pd.np.nan, 7.946, inplace=True)
# Method 6
pop[(pop['Log GDP per capita'].isna())][(pop['Country name'])=='Somalia'].mask(pop['Log GDP per capita']==np.nan, 7.946, inplace=True)
# Method 7
pop.loc([(pop['Country name']=='Somalia')]['Log GDP per capita'])=7.946 

How can I improve that?

Kaggle Notebook

CodePudding user response:

Use .loc:

mask = pop['Log GDP per capita'].isna() & pop['Country name'].eq('Somalia')
pop.loc[mask, 'Log GDP per capita'] = 8

CodePudding user response:

Maybe you will try to replace Nan values per column

Replace NaN values with specific value per column

Or study id you need this Nan Values in your dataframe

  •  Tags:  
  • Related