Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.

Examples
Combine using a simple function that chooses the smaller column:

In [1]:
import numpy as np
import pandas as pd
In [2]:
df1 = pd.DataFrame({'X': [0, 0], 'Y': [5, 5]})
df2 = pd.DataFrame({'X': [2, 2], 'Y': [4, 4]})
take_smaller = lambda s1, s2: s1 if s1.sum() < s2.sum() else s2
df1.combine(df2, take_smaller)
Out[2]:
X Y
0 0 4
1 0 4

Using a true element-wise combine function:

In [3]:
df1 = pd.DataFrame({'X': [5, 0], 'Y': [4, 6]})
df2 = pd.DataFrame({'X': [2, 2], 'Y': [3, 3]})
df1.combine(df2, np.minimum)
Out[3]:
X Y
0 2 3
1 0 3

Using fill_value fills Nones prior to passing the column to the merge function:

In [4]:
df1 = pd.DataFrame({'X': [0, 0], 'Y': [None, 6]})
df2 = pd.DataFrame({'X': [2, 2], 'Y': [3, 3]})
df1.combine(df2, take_smaller, fill_value=-5)
Out[4]:
X Y
0 0 -5.0
1 0 6.0

If the same element in both dataframes is None, that None is preserved:

In [5]:
df1 = pd.DataFrame({'X': [0, 0], 'Y': [None, 6]})
df2 = pd.DataFrame({'X': [2, 2], 'Y': [None, 3]})
df1.combine(df2, take_smaller, fill_value=-5)
Out[5]:
X Y
0 0 -5.0
1 0 3.0

Use of overwrite and behavior when the axis differ between the dataframes:

In [6]:
df1 = pd.DataFrame({'X': [0, 0], 'Y': [6, 6]})
df2 = pd.DataFrame({'Y': [5, 5], 'Z': [-10, 2], }, index=[1, 2])
df1.combine(df2, take_smaller)
Out[6]:
X Y Z
0 NaN NaN NaN
1 NaN 5.0 -10.0
2 NaN 5.0 2.0
In [7]:
df1.combine(df2, take_smaller, overwrite=False)
Out[7]:
X Y Z
0 0.0 NaN NaN
1 0.0 5.0 -10.0
2 NaN 5.0 2.0

Demonstrating the preference of the passed in dataframe:

In [8]:
df2 = pd.DataFrame({'Y': [4, 4], 'Z': [2, 2], }, index=[1, 2])
df2.combine(df1, take_smaller)
Out[8]:
X Y Z
0 0.0 NaN NaN
1 0.0 4.0 NaN
2 NaN 4.0 NaN
In [9]:
df2.combine(df1, take_smaller, overwrite=False)
Out[9]:
X Y Z
0 0.0 NaN NaN
1 0.0 4.0 2.0
2 NaN 4.0 2.0