pandas.DataFrame.combine() Function | How to combine pandas with function
pandas.DataFrame.combine() Function: Perform column-wise combine with another DataFrame based on a passed function. 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.
Syntax: DataFrame.combine(other, func, fill_value=None, overwrite=True)
Parameters:
other : DataFrame
The DataFrame to merge column-wise.
func : function
Function that takes two series as inputs and return a Series or a scalar. Used to merge the two dataframes column by columns.
fill_value : scalar value, default None
The value to fill NaNs with prior to passing any column to the merge func.
overwrite : boolean, default True
If True, columns in self that do not exist in other will be overwritten with NaNs.
Returns:
result : DataFrame
Example program on pandas.DataFrame.combine() Function:
import numpy as np
import pandas as pd
df1 = pd.DataFrame({'A': [5, 2], 'B': [4, 8]})
df2 = pd.DataFrame({'A': [1, 5], 'B': [9, 9]})
df1.combine(df2, np.maximum)
print(df1)
Output:
A B 0 5 4 1 2 8