Pandas DataFrame.sub


Pandas DataFrame.sub

The DataFrame.sub method in pandas is used to perform element-wise subtraction between a DataFrame and another DataFrame, Series, or scalar value. It provides flexibility with options for handling missing data and aligning data along specific axes.


Syntax

The syntax for DataFrame.sub is:

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

Here, DataFrame refers to the pandas DataFrame being subtracted from.


Parameters

ParameterDescription
otherDataFrame, Series, or scalar value to subtract from the DataFrame.
axisSpecifies whether to align the subtraction along rows (axis=0) or columns (axis=1). Defaults to 'columns'.
levelUsed to specify a particular level for subtraction in a MultiIndex DataFrame.
fill_valueValue to use for missing data when aligning other with the DataFrame.

Returns

A new DataFrame containing the element-wise differences.


Examples

Subtracting a Scalar Value

Subtract a scalar value from all elements in the DataFrame.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'Math': [90, 80, 85],
    'Science': [95, 85, 80]
}
df = pd.DataFrame(data, index=['Arjun', 'Ram', 'Priya'])

# Subtract a scalar value
print("Subtracting 5 from all elements in the DataFrame:")
df_sub = df.sub(5)
print(df_sub)

Output

Subtracting 5 from all elements in the DataFrame:
       Math  Science
Arjun     85       90
Ram       75       80
Priya     80       75

Subtracting Another DataFrame

Subtract another DataFrame of the same shape.

Python Program

import pandas as pd

# Create two DataFrames
data1 = {
    'Math': [90, 80, 85],
    'Science': [95, 85, 80]
}
data2 = {
    'Math': [10, 20, 15],
    'Science': [5, 10, 15]
}
df1 = pd.DataFrame(data1, index=['Arjun', 'Ram', 'Priya'])
df2 = pd.DataFrame(data2, index=['Arjun', 'Ram', 'Priya'])

# Subtract the second DataFrame from the first
print("Subtracting one DataFrame from another:")
df_sub = df1.sub(df2)
print(df_sub)

Output

Subtracting one DataFrame from another:
       Math  Science
Arjun     80       90
Ram       60       75
Priya     70       65

Subtracting with Missing Data

Use the fill_value parameter to handle missing data during subtraction.

Python Program

import pandas as pd

# Create two DataFrames with missing data
data1 = {
    'Math': [90, 80, 85],
    'Science': [95, 85, None]
}
data2 = {
    'Math': [10, 20, None],
    'Science': [5, None, 15]
}
df1 = pd.DataFrame(data1, index=['Arjun', 'Ram', 'Priya'])
df2 = pd.DataFrame(data2, index=['Arjun', 'Ram', 'Priya'])

# Subtract with fill_value
print("Subtracting with fill_value=0:")
df_sub = df1.sub(df2, fill_value=0)
print(df_sub)

Output

Subtracting with fill_value=0:
       Math  Science
Arjun   80.0     90.0
Ram     60.0     85.0
Priya   85.0     65.0

Subtracting Along Rows

Use the axis=0 parameter to subtract along rows instead of columns.

Python Program

import pandas as pd

# Create a DataFrame and a Series
data = {
    'Math': [90, 80, 85],
    'Science': [95, 85, 80]
}
series = pd.Series([10, 5, 0], index=['Arjun', 'Ram', 'Priya'])
df = pd.DataFrame(data, index=['Arjun', 'Ram', 'Priya'])

# Subtract the Series along rows
print("Subtracting a Series along rows:")
df_sub = df.sub(series, axis=0)
print(df_sub)

Output

Subtracting a Series along rows:
       Math  Science
Arjun     80       85
Ram       75       80
Priya     85       80

Summary

In this tutorial, we explored the DataFrame.sub method in pandas. Key takeaways include:

  • Using sub to perform element-wise subtraction between DataFrames, Series, or scalars.
  • Handling missing data with fill_value.
  • Customizing subtraction along rows or columns using the axis parameter.

The DataFrame.sub method is a flexible tool for performing subtraction in pandas.


Python Libraries