Home > Software engineering >  Replace N/A values with values in a corresponding values and a string
Replace N/A values with values in a corresponding values and a string

Time:09-18

I want to replace NaN values in one column with values in another column and a string. Sample:

    date_added  release_year
4   NaN         2019

I want to replace NaN values in the 'date_added' column with the string 'January 1, {release_year}'. I tried to use the following code:

for i in df.index:
    df['date_added'].fillna('January 1, {}'.format(df.loc[i, 'release_year']), inplace = True)

However, the result seems not correct:

        date_added        release_year
5339    January 1, 2019   2016

Anybody can provide me with a solution for this?

Thanks all!

CodePudding user response:

The method fillna() iterates over the row indeces, so you don't need to use for loop. In your case only the first iteration of the loop does something, after that there is no NaNs anymore. Try this:

df['date_added'].fillna(df['release_year'].apply(lambda x: f'January 1, {x}'), inplace = True)

Or this (does the same):

df['date_added'].fillna('January 1, '   df['release_year'].astype(str), inplace = True)

CodePudding user response:

import pandas as pd
import numpy as np

df = pd.DataFrame(data={'date_added': [np.nan, np.nan, np.nan],
                        'release_year': [2015, 2019, 2018]})
print(df)

   date_added  release_year
0         NaN          2015
1         NaN          2019
2         NaN          2018

df['date_added'] = df['date_added'].fillna('January 1, '   (df['release_year']).astype(str))

print(df)

        date_added  release_year
0  January 1, 2015          2015
1  January 1, 2019          2019
2  January 1, 2018          2018

CodePudding user response:

Another way is using np.where:

import numpy as np
import pandas as pd


df['date_added'] = np.where(df['date_added'].isnull(), 'January 1 '   df['release_year'].astype(str),
                            df['date_added'])


       date_added  release_year
0  January 1 2019          2019
1     21 Sep 2021          2021
2  January 1 2022          2022
  • Related