Search code examples
pandasjoinreplacenanfillna

How can I replace NaN values in DataFrame from another table?


I have a DataFrame 'df'

enter image description here

And the second is 'nan_gdp'

enter image description here

How can I fill NaN gdp in 'nan_gdp' by using my first DataFrame 'df'. Also, in the first df I dont have all countries, it means that there are some countries which are in 'nan_gdp' but not in 'df'


Solution

  • Use Series.fillna by mapped values from df by Series.map:

    s = df.set_index('Country')['GDP ($M)']
    waste['GDP ($M)'] = waste['GDP ($M)'].fillna(waste['Country'].map(s))