Replacing null values with values in another data set

Hello!

I ran into a bit of an issue and I’m wondering if anyone can help me out.

So, suppose I had a dataset, df1, like this:

Year Temperature
1972 25
1973 27
1974 NA
1975 23
1976 29
1977 NA
1978 25

And another dataset, df2, like this:

Year Temperature
1972 22
1973 28
1974 21
1975 25
1976 29
1977 23
1978 27

Now, I want to get rid of the null values in df1 but I’d like to do so by replacing them with whatever value is found in df2 for the corresponding year.

So in the end, df1 should look like this:

Year Temperature
1972 25
1973 27
1974 21
1975 23
1976 29
1977 23
1978 25

Is anyone aware of a method I can use to fill my null values this way?

Thank you in advance for the help!

Hi @Johnsonk51502

You can do that using pd.fillna() method.

Basically you’ll do something like this

df1.fillna(df2['Temperature'], inplace=True) 

Check the pandas documentation to find good examples about it
Hope this helps you
Good luck!

2 Likes

Thank you! I knew there was an easier way to do this, I appreciate the help!