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 −
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 |
Example
The following example demonstrates how to applies the all arithmetical operators on a Pandas DataFrame with a scalar value.
import pandas as pd
data = {'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]}
df = pd.DataFrame(data)
print("Input DataFrame:\n", df)
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:
|
A |
B |
0 |
1 |
5 |
1 |
2 |
6 |
2 |
3 |
7 |
3 |
4 |
8 |
Addition:
|
A |
B |
0 |
3 |
7 |
1 |
4 |
8 |
2 |
5 |
9 |
3 |
6 |
10 |
Subtraction:
|
A |
B |
0 |
-1 |
3 |
1 |
0 |
4 |
2 |
1 |
5 |
3 |
2 |
6 |
Multiplication:
|
A |
B |
0 |
2 |
10 |
1 |
4 |
12 |
2 |
6 |
14 |
3 |
8 |
16 |
Division:
|
A |
B |
0 |
0.5 |
2.5 |
1 |
1.0 |
3.0 |
2 |
1.5 |
3.5 |
3 |
2.0 |
4.0 |
Exponentiation:
|
A |
B |
0 |
1 |
25 |
1 |
4 |
36 |
2 |
9 |
49 |
3 |
16 |
64 |
Modulus:
|
A |
B |
0 |
1 |
1 |
1 |
0 |
0 |
2 |
1 |
1 |
3 |
0 |
0 |
Floor Division:
|
A |
B |
0 |
0 |
2 |
1 |
1 |
3 |
2 |
1 |
3 |
3 |
2 |
4 |
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
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])
print("DataFrame 1:\n", df1)
print("\nDataFrame 2:\n", df2)
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:
|
A |
B |
0 |
1 |
5 |
1 |
2 |
6 |
2 |
3 |
7 |
3 |
4 |
8 |
DataFrame 2:
|
A |
B |
1 |
10 |
50 |
2 |
20 |
60 |
3 |
30 |
70 |
Addition of Two DataFrames:
|
A |
B |
0 |
NaN |
NaN |
1 |
12.0 |
56.0 |
2 |
23.0 |
67.0 |
3 |
34.0 |
78.0 |
Subtraction of Two DataFrames:
|
A |
B |
0 |
NaN |
NaN |
1 |
-8.0 |
-44.0 |
2 |
-17.0 |
-53.0 |
3 |
-26.0 |
-62.0 |
Multiplication of Two DataFrames:
|
A |
B |
0 |
NaN |
NaN |
1 |
22.0 |
300.0 |
2 |
60.0 |
420.0 |
3 |
120.0 |
560.0 |
Division of Two DataFrames:
|
A |
B |
0 |
NaN |
NaN |
1 |
0.200000 |
0.120000 |
2 |
0.150000 |
0.116667 |
3 |
0.133333 |
0.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.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. |