Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Pandas DataFrame combine() Method

In this tutorial, we will learn the python pandas DataFrame.combine() method. It performs column-wise combine with another DataFrame. It combines a DataFrame with other DataFrame using func to element-wise combine columns. The row and column indexes of the resulting DataFrame will be the union of the two.

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

Syntax

DataFrame.combine(other, func, fill_value=None, overwrite=True)

Parameters

other: It represents the other DataFrame to merge column-wise.

func: It represents the function that takes two series as inputs and returns a Series or a scalar. It is used to merge the two dataframes column by column.

fill_value: It is a scalar value, default None. The value to fill NaNs with prior to passing any column to the merge func.

overwrite: It represents the bool(True or False), and the default value is True. If True, columns in self that do not exist in other will be overwritten with NaNs.

Example 1: Combine DataFrame with another DataFrame using DataFrame.combine() Method

The below example shows how to combine two dataframe using the DataFrame.combine() method that chooses the smaller column.

import pandas as pd
df1 = pd.DataFrame({'A': [2, 0, 5], 'B': [2, 0, 5]})
df2 = pd.DataFrame({'A': [3, 1,10], 'B': [4, 3, -4]})
take_smaller = lambda s1, s2: s1 if s1.sum() < s2.sum() else s2
print(df1.combine(df2, take_smaller))

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


A B
0 2 4
1 0 3
2 5 -4

Example 2: Combine DataFrame with another DataFrame using DataFrame.combine() Method

The below example is similar to the previous one. Combine two dataframes by giving different function to the DataFrame.combine() function.

import pandas as pd
import numpy as np
df1 = pd.DataFrame({'A': [2, 0, 5], 'B': [2, 2, -0.25]})
df2 = pd.DataFrame({'A': [3, 1,10], 'B': [3, 3, -4]})
print(df1.combine(df2, np.minimum))
print(df1.combine(df2, np.maximum))

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


A B
0 2 2.0
1 0 2.0
2 5 -4.0
A B
0 3 3.00
1 1 3.00
2 10 -0.25

Example 3: Combine DataFrames by filling in missing values

The below example shows the usage of fill_value in DataFrame.combine() method that fills Nones prior to passing the column to the merge function.

import pandas as pd
import numpy as np
df1 = pd.DataFrame({'A': [2, 0, 5], 'B': [2, None, -0.25]})
df2 = pd.DataFrame({'A': [3, 1,None], 'B': [3, 3, -4]})
print(df1.combine(df2, np.minimum, fill_value=2))
print(df1.combine(df2, np.maximum,fill_value=-5))

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


A B
0 2 2.0
1 0 2.0
2 2 -4.0
A B
0 3 3.00
1 1 3.00
2 5 -0.25

Example: Combine if the Same Element in both dataframes is None

If the same element in both dataframes is None, that None is preserved. The below example shows the same.

import pandas as pd
import numpy as np
df1 = pd.DataFrame({'A': [0, 0], 'B': [None, 4]})
df2 = pd.DataFrame({'A': [1, 1], 'B': [None, 3]})
print(df1.combine(df2, np.minimum, fill_value=-2))

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


A B
0 0 -2.0
1 0 3.0

Conclusion

In this tutorial, we learned the Python pandas DataFrame.combine() method. We learned the syntax, parameters of the DataFrame.combine() method and combined two dataframes with different functions and by using different parameters.