Pandas Tutorial Pandas References

Pandas - DataFrame Arithmetic Functions



The Pandas package contains a number of arithmetic functions which provides all the functionality required for various arithmetic operations on a DataFrame and Series. Below mentioned are the most frequently used arithmetic functions.

FunctionsDescription
add() Add arguments element-wise.
sub() Subtract arguments, element-wise.
mul() Multiply arguments, element-wise.
div() Divide arguments, element-wise.
floordiv() Perform integer division of arguments, element-wise.
mod() Calculate modulo of arguments, element-wise.
pow() Calculate exponential power of dataframe and argument, element-wise.

Lets discuss these functions in detail:

Basic Arithmetic Operations

Basic arithmetic operations can be performed on a given DataFrame, element-wise using add(), sub(), mul(), div(), floordiv() and mod() functions. It is equivalent to using operator like +, -, *, /, // or % but with support to substitute a fill_value for missing data as one of the parameters. The syntax for using these functions are given below:

Syntax

DataFrame.add(other, axis='columns', level=None, fill_value=None)
DataFrame.sub(other, axis='columns', level=None, fill_value=None)
DataFrame.mul(other, axis='columns', level=None, fill_value=None)
DataFrame.div(other, axis='columns', level=None, fill_value=None)
DataFrame.floordiv(other, axis='columns', level=None, fill_value=None)
DataFrame.mod(other, axis='columns', level=None, fill_value=None)

Parameters

other Required. Specify any single or multiple element data structure, or list-like object.
axis Optional. Specify whether to compare by the index (0 or 'index') or columns (1 or 'columns'). For Series input, axis to match Series index on. Default is 'columns'.
level Optional. Specify int or label to broadcast across a level, matching Index values on the passed MultiIndex level. Default is None.
fill_value Optional. Specify value to fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment. If data in both corresponding DataFrame locations is missing the result will be missing. Default is None.

Example:

In the example below, a DataFrame df is created. The various arithmetic functions are used on this DataFrame.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2],
  "Salary": [60, 62, 65]},
  index= ["John", "Marry", "Sam"]
)

print("The DataFrame is:")
print(df)

#adding 3 to all entries of the DataFrame
print("\ndf.add(3) returns:")
print(df.add(3))

#subtracting 2 from all entries of Bonus column
#subtracting 10 from all entries of Salary column
print("\ndf.sub([2,10]) returns:")
print(df.sub([2,10]))

#multiplying by 2 to all entries of the DataFrame
print("\ndf.mul(3) returns:")
print(df.mul(3))

#dividing by 2 to all entries of Bonus column
#dividing by 10 to all entries of Salary column
print("\ndf.div([2,10]) returns:")
print(df.div([2,10]))

The output of the above code will be:

The DataFrame is:
       Bonus  Salary
John       5      60
Marry      3      62
Sam        2      65

df.add(3) returns:
       Bonus  Salary
John       8      63
Marry      6      65
Sam        5      68

df.sub([2,10]) returns:
       Bonus  Salary
John       3      50
Marry      1      52
Sam        0      55

df.mul(3) returns:
       Bonus  Salary
John      15     180
Marry      9     186
Sam        6     195

df.div([2,10]) returns:
       Bonus  Salary
John     2.5     6.0
Marry    1.5     6.2
Sam      1.0     6.5

Example:

Similarly, floordiv() and mod() functions can be used on a DataFrame. Consider the example below.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2],
  "Salary": [60, 62, 65]},
  index= ["John", "Marry", "Sam"]
)

print("The DataFrame is:")
print(df)

#dividing all entries of the DataFrame by 2
print("\ndf.floordiv(2) returns:")
print(df.floordiv(2))

#Modulo when Bonus column is divided by 2
#Modulo when Salary column is divided by 10
print("\ndf.mod([2,10]) returns:")
print(df.mod([2,10]))

The output of the above code will be:

The DataFrame is:
       Bonus  Salary
John       5      60
Marry      3      62
Sam        2      65

df.floordiv(2) returns:
       Bonus  Salary
John       2      30
Marry      1      31
Sam        1      32

df.mod([2,10]) returns:
       Bonus  Salary
John       1       0
Marry      1       2
Sam        0       5

DataFrame.pow() function

The Pandas pow() function returns exponential power of dataframe and other, element-wise. It is equivalent to dataframe ** other, but with support to substitute a fill_value for missing data as one of the parameters.

Syntax

DataFrame.pow(other, axis='columns', level=None, fill_value=None)

Parameters

other Required. Specify any single or multiple element data structure, or list-like object.
axis Optional. Specify whether to compare by the index (0 or 'index') or columns (1 or 'columns'). For Series input, axis to match Series index on. Default is 'columns'.
level Optional. Specify int or label to broadcast across a level, matching Index values on the passed MultiIndex level. Default is None.
fill_value Optional. Specify value to fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment. If data in both corresponding DataFrame locations is missing the result will be missing. Default is None.

Example:

In the example below, a DataFrame df is created. The pow() function is used to calculate exponential power of dataframe and argument.

import pandas as pd
import numpy as np

df = pd.DataFrame({
  "Bonus": [5, 3, 2, 4],
  "Salary": [60, 62, 65, 59]},
  index= ["John", "Marry", "Sam", "Jo"]
)

print("The DataFrame is:")
print(df)

#Squaring all entries of the DataFrame
print("\ndf.pow(2) returns:")
print(df.pow(2))

#Cubing all entries of Bonus column
#Squaring all entries of Salary column
print("\ndf.pow([3,2]) returns:")
print(df.pow([3,2]))

The output of the above code will be:

The DataFrame is:
       Bonus  Salary
John       5      60
Marry      3      62
Sam        2      65
Jo         4      59

df.pow(2) returns:
       Bonus  Salary
John      25    3600
Marry      9    3844
Sam        4    4225
Jo        16    3481

df.pow([3,2]) returns:
       Bonus  Salary
John     125    3600
Marry     27    3844
Sam        8    4225
Jo        64    3481