Some binary functions such as, add, sub, mul, div, mod, and pow, perform common arithmetic operations involving two DataFrames or series.
The following example shows the addition of two DataFrames. One of the DataFrames has the shape (2,3) while the other has the shape (1,3). The add function performs an elementwise addition. When a corresponding element is missing in any of the DataFrames, the missing values are filled with NaNs:
df_1 = pd.DataFrame([[1,2,3],[4,5,6]])
df_2 = pd.DataFrame([[6,7,8]])
df_1.add(df_2)
The following is the output:
Adding two DataFrames elementwise
Instead of using NaNs, we can choose to fill it with any value using the fill_value argument. Let's explore this through the mul function for multiplication:
df_1.mul(df_2, fill_value = 0)
The following is the output:
The fill_value parameter in binary operators...