Binary operations on DataFrames and series

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, ...

Get Mastering pandas - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.