Regid
Regid

Reputation: 73

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'

Upvotes: 1

Views: 210

Answers (1)

jezrael
jezrael

Reputation: 863216

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))

Upvotes: 1

Related Questions