Python Pandas - Arithmetic Operations on DataFrame



Pandas DataFrame is a two-dimensional, labeled data structure that allows for efficient data manipulation and analysis. One of the primary features of Pandas is its ability to perform vectorized arithmetic operations on DataFrames. This means you can apply mathematical operations without using loop through elements manually.

Applying arithmetic operations in Pandas allows you to manipulate data quickly and efficiently, whether you're working with a single DataFrame or performing operations between multiple DataFrames.

In this tutorial, we will learn how to apply arithmetic operations like addition, subtraction, multiplication, and division on Pandas DataFrames.

Arithmetic Operations on DataFrame with Scalar Value

You can perform arithmetic operations on a DataFrame with scalar values directly. These operations are applied element-wise, meaning that every value in the DataFrame is affected by the arithmetic operation.

Following is the list of commonly used arithmetic operators on Pandas DataFrame −

OperationExample with OperatorDescription
Additiondf + 2Adds 2 to each element of the DataFrame
Subtractiondf - 2Subtracts 2 from each element
Multiplicationdf * 2Multiplies each element by 2
Divisiondf / 2Divides each element by 2
Exponentiationdf ** 2Raises each element to the power of 2
Modulusdf % 2Finds the remainder when divided by 2
Floor Divisiondf // 2Divides and floors the quotient

Example

The following example demonstrates how to applies the all arithmetical operators on a Pandas DataFrame with a scalar value.

 import pandas as pd # Create a sample DataFrame data = {'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]} df = pd.DataFrame(data) # Display the input DataFrame print("Input DataFrame:\n", df) # Perform arithmetic operations print("\nAddition:\n", df + 2) print("\nSubtraction:\n", df - 2) print("\nMultiplication:\n", df * 2) print("\nDivision:\n", df / 2) print("\nExponentiation:\n", df ** 2) print("\nModulus:\n", df % 2) print("\nFloor Division:\n", df // 2) 

Following is the output of the above code −

 Input DataFrame: 
AB
015
126
237
348
Addition:
AB
037
148
259
3610
Subtraction:
AB
0-13
104
215
326
Multiplication:
AB
0210
1412
2614
3816
Division:
AB
00.52.5
11.03.0
21.53.5
32.04.0
Exponentiation:
AB
0125
1436
2949
31664
Modulus:
AB
011
100
211
300
Floor Division:
AB
002
113
213
324

Arithmetic Operations Between Two DataFrames

Pandas allows you to apply arithmetic operators between two DataFrames efficiently. These operations are applied element-wise, meaning corresponding elements in both DataFrames are used in calculations.

When performing arithmetic operations on two DataFrames, Pandas aligns them based on their index and column labels. If a particular index or column is missing in either DataFrame, the result for those entries will be NaN, indicating missing values.

Example

This example demonstrates applying the arithmetic operations on two DataFrame. These operations include addition, subtraction, multiplication, and division of two DataFrame.

 import pandas as pd # Create two DataFrames df1 = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]}) df2 = pd.DataFrame({'A': [10, 20, 30], 'B': [50, 60, 70]}, index=[1, 2, 3]) # Display the input DataFrames print("DataFrame 1:\n", df1) print("\nDataFrame 2:\n", df2) # Perform arithmetic operations print("\nAddition of Two DataFrames:\n", df1 + df2) print("\nSubtraction of Two DataFrames:\n", df1 - df2) print("\nMultiplication of Two DataFrames:\n", df1 * df2) print("\nDivision of Two DataFrames:\n", df1 / df2) 

Following is the output of the above code −

 DataFrame 1: 
AB
015
126
237
348
DataFrame 2:
AB
11050
22060
33070
Addition of Two DataFrames:
AB
0NaNNaN
112.056.0
223.067.0
334.078.0
Subtraction of Two DataFrames:
AB
0NaNNaN
1-8.0-44.0
2-17.0-53.0
3-26.0-62.0
Multiplication of Two DataFrames:
AB
0NaNNaN
122.0300.0
260.0420.0
3120.0560.0
Division of Two DataFrames:
AB
0NaNNaN
10.2000000.120000
20.1500000.116667
30.1333330.114286

Arithmetic Functions in Pandas

In addition to the above operators, Pandas provides various functions to perform arithmetic operations on Pandas Data structure, which can handle missing values efficiently and provides additional options for customization, like selecting the axis and specifying levels.

S.NoFunctionDescription
1add(other[, axis, level, fill_value])Element-wise addition (binary operator +).
2sub(other[, axis, level, fill_value])Element-wise subtraction (binary operator -).
3mul(other[, axis, level, fill_value])Element-wise multiplication (binary operator *).
4div(other[, axis, level, fill_value])Element-wise floating division (binary operator /).
5truediv(other[, axis, level, ...])Element-wise floating division (binary operator /).
6floordiv(other[, axis, level, ...])Element-wise integer division (binary operator //).
7mod(other[, axis, level, fill_value])Element-wise modulo operation (binary operator %).
8pow(other[, axis, level, fill_value])Element-wise exponential power (binary operator **).
9dot(other)Matrix multiplication with another DataFrame or array.
10radd(other[, axis, level, fill_value])Reverse element-wise addition.
11rsub(other[, axis, level, fill_value])Reverse element-wise subtraction.
12rmul(other[, axis, level, fill_value])Reverse element-wise multiplication.
13rdiv(other[, axis, level, fill_value])Reverse element-wise floating division.
14rfloordiv(other[, axis, level, ...])Reverse element-wise integer division.
15rmod(other[, axis, level, fill_value])Reverse element-wise modulo operation.
16rpow(other[, axis, level, fill_value])Reverse element-wise exponential power.
Advertisements
close