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.
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 −
| Operation | Example with Operator | Description |
|---|---|---|
| Addition | df + 2 | Adds 2 to each element of the DataFrame |
| Subtraction | df – 2 | Subtracts 2 from each element |
| Multiplication | df * 2 | Multiplies each element by 2 |
| Division | df / 2 | Divides each element by 2 |
| Exponentiation | df ** 2 | Raises each element to the power of 2 |
| Modulus | df % 2 | Finds the remainder when divided by 2 |
| Floor Division | df // 2 | Divides and floors the quotient |
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)
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.
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)
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.No | Function | Description |
|---|---|---|
| 1 | add(other[, axis, level, fill_value]) | Element-wise addition (binary operator +). |
| 2 | sub(other[, axis, level, fill_value]) | Element-wise subtraction (binary operator -). |
| 3 | mul(other[, axis, level, fill_value]) | Element-wise multiplication (binary operator *). |
| 4 | div(other[, axis, level, fill_value]) | Element-wise floating division (binary operator /). |
| 5 | truediv(other[, axis, level, …]) | Element-wise floating division (binary operator /). |
| 6 | floordiv(other[, axis, level, …]) | Element-wise integer division (binary operator //). |
| 7 | mod(other[, axis, level, fill_value]) | Element-wise modulo operation (binary operator %). |
| 8 | pow(other[, axis, level, fill_value]) | Element-wise exponential power (binary operator **). |
| 9 | dot(other) | Matrix multiplication with another DataFrame or array. |
| 10 | radd(other[, axis, level, fill_value]) | Reverse element-wise addition. |
| 11 | rsub(other[, axis, level, fill_value]) | Reverse element-wise subtraction. |
| 12 | rmul(other[, axis, level, fill_value]) | Reverse element-wise multiplication. |
| 13 | rdiv(other[, axis, level, fill_value]) | Reverse element-wise floating division. |
| 14 | rfloordiv(other[, axis, level, …]) | Reverse element-wise integer division. |
| 15 | rmod(other[, axis, level, fill_value]) | Reverse element-wise modulo operation. |
| 16 | rpow(other[, axis, level, fill_value]) | Reverse element-wise exponential power. |
