Signup/Sign In
PUBLISHED ON: MARCH 15, 2021

Pandas DataFrame combine_first() Method

In this tutorial, we will learn the Python pandas DataFrame.combine_first() method. It updates null elements with value in the same location in other and returns the DataFrame. It combines two DataFrame objects by filling null values in one DataFrame with non-null values from another DataFrame. The row and column indexes of the resulting DataFrame will be the union of the two.

The below shows the syntax of the DataFrame.combine_first() method.

Syntax

DataFrame.combine_first(other)

Parameters

other: It represents the other provided DataFrame to use to fill null values.

Example 1: Combine DataFrames using DataFrame.combine_first() Method

The below example shows how the DataFrame.combine_first() method.

#importing pandas as pd
import pandas as pd
df1 = pd.DataFrame({'A': [None, 0], 'B': [None, 4]})
df2 = pd.DataFrame({'A': [1, 1], 'B': [3, 3]})
print(df1.combine_first(df2))

Once we run the program we will get the following result.


A B
0 1.0 3.0
1 0.0 4.0

Example 2: Combine DataFrames using DataFrame.combine_first() Method

The below example is similar to the previous one, in this example try to add df1 DataFrame with df2 DataFrame and see the difference.

#importing pandas as pd
import pandas as pd
df1 = pd.DataFrame({'A': [None, 0], 'B': [None, 4]})
df2 = pd.DataFrame({'A': [1, 1], 'B': [3, 3]})
print(df2.combine_first(df1))

Once we run the program we will get the following result.


A B
0 1 3
1 1 3

Example 3: Combine If the same element in both dataframes is None

If the same element in both dataframes is None it combines two DataFrame objects by the same null values.

#importing pandas as pd
import pandas as pd
df1 = pd.DataFrame({'A': [None, 0], 'B': [None, 4]})
df2 = pd.DataFrame({'A': [None, 1], 'B': [None, 3]})
print(df1.combine_first(df2))

Once we run the program we will get the following result.


A B
0 NaN NaN
1 0.0 4.0

Example 4: Combine DataFrames using DataFrame.combine_first() Method

If the location of that null value does not exist in other, Null values still persist in the DataFrame.

import pandas as pd
df1 = pd.DataFrame({'A': [None, 0], 'B': [4, None]})
df2 = pd.DataFrame({'B': [3, 3], 'C': [1, 1]}, index=[1, 2])
print(df1.combine_first(df2))

Once we run the program we will get the following result.


A B C
0 NaN 4.0 NaN
1 0.0 3.0 1.0
2 NaN 3.0 1.0

Conclusion

In this tutorial, we learned the Python pandas DataFrame.combine_first() method. We solved examples and understood the DataFrame.combine_first() method effectively fills the null values in the DataFrame as well as supply values for indices and columns that didn't exist in the first DataFrame.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.