Pandas sub – Python Pandas DataFrame sub() Function

Pandas sub: Python is an excellent language for data analysis, due to a strong ecosystem of data-centric Python tools. One of these packages is Pandas, which makes importing and analyzing data a lot easier.

Pandas DataFrame sub() Function:

The sub() function of the Pandas module returns element-by-element subtraction of dataframe and other. It’s similar to dataframe – other, but with the ability to provide a fill_value as one of the parameters to replace missing data.

Syntax:

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

Parameters

other: This is required. It indicates any single or multiple element data structure or list-like object. The type of this may be scalar, sequence, Series, or DataFrame

axis: This is optional. It indicates whether to compare by index (0 or ‘index’) and columns (1 or ‘columns’). axis to match Series index on, for Series input. The default value is ‘columns.’

level: This is optional. To broadcast over a level, specify an int or a label that matches the Index values on the passed MultiIndex level. None is the default value. The type of this may be int or label.

fill_value: This is optional. It indicates a value to fill in any current missing (NaN) values, as well as any new elements required for DataFrame alignment to succeed. The result will be missing if data in both corresponding DataFrame locations is missing. The type of this may be float or None. None is the default value.

Return Value:

The result of the arithmetic operation is returned.

Pandas DataFrame sub() Function in Python

Example1

Here, to subtract a scalar value from the entire DataFrame, we used the sub() function.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe
  • Store it in a variable.
  • Print the given dataframe
  • Apply sub() function to the dataframe by passing some random number to it and print the result.
  • Here it subtracts 1 from the entire dataframe.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "student_rollno": [1, 2, 3, 4],
  "student_marks": [80, 35, 25, 90]},
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Apply sub() function to the dataframe by passing some random number
# to it and print the result.
# Here it subtracts 1 from the entire dataframe.
print("The result dataframe after subtracting 1 from the entire dataframe:")
print(data_frme.sub(1))

Output:

The given Dataframe:
        student_rollno  student_marks
virat                1             80
nick                 2             35
jessy                3             25
sindhu               4             90

The result dataframe after subtracting 1 from the entire dataframe:
        student_rollno  student_marks
virat                0             79
nick                 1             34
jessy                2             24
sindhu               3             89

Example2

Here, the ‘other’ argument can be provided as a list to subtract different scalar values from different columns.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe
  • Store it in a variable.
  • Print the given dataframe
  • Apply sub() function on the dataframe by passing list as argument to it which subtracts corresponding list elements from the dataframe and print the result.
  • Here it subtracts 2 from the student_rollno column of the dataframe and 10 from the student_marks column of the dataframe.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "student_rollno": [1, 2, 3, 4],
  "student_marks": [80, 35, 25, 90]},
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Apply sub() function on the dataframe by passing list as argument to it 
# which subtracts corresponding list elements from the dataframe and print the result.
# Here it subtracts 2 from the student_rollno column of the dataframe.
# and 10 from the student_marks column of the dataframe.
print("The dataframe after subtracting 2 from student_rollno, 10 from student_marks columns:")
print(data_frme.sub([2, 10]))

Output:

The given Dataframe:
        student_rollno  student_marks
virat                1             80
nick                 2             35
jessy                3             25
sindhu               4             90

The dataframe after subtracting 2 from student_rollno, 10 from student_marks columns:
        student_rollno  student_marks
virat               -1             70
nick                 0             25
jessy                1             15
sindhu               2             80

Example3

Here, sub() function is used on specific columns rather than the entire DataFrame.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe
  • Store it in a variable.
  • Print the given dataframe
  • Apply sub() function to the specified column of dataframe by passing some random number to it and print the result.
  • Here it subtracts 5 from the student_rollno column of the dataframe.
  • Apply sub() function to the specified columns of dataframe by which subtracts corresponding list elements from the dataframe columns and print the result.
  • Here it subtracts 2 from the student_rollno column of the dataframe and 10 from the student_marks column of the dataframe.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "student_rollno": [1, 2, 3, 4],
  "student_marks": [80, 35, 25, 90]},
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()
# Apply sub() function to the specified column of dataframe by passing some random number
# to it and print the result.
# Here it subtracts 5 from the student_rollno column of the dataframe.
print("The result dataframe after subtracting 5 from the student_rollno columns:")
print(data_frme["student_rollno"].sub(5))
print()

# Apply sub() function to the specified columns of dataframe by which subtracts corresponding 
# list elements from the dataframe columns and print the result.
# Here it subtracts 2 from the student_rollno column of the dataframe.
# and 10 from the student_marks column of the dataframe.
print("The dataframe after subtracting 2 from student_rollno, 10 from student_marks columns:")
print(data_frme[["student_rollno", "student_marks"]].sub([2, 10]))

Output:

The given Dataframe:
        student_rollno  student_marks
virat                1             80
nick                 2             35
jessy                3             25
sindhu               4             90

The result dataframe after subtracting 5 from the student_rollno columns:
virat    -4
nick     -3
jessy    -2
sindhu   -1
Name: student_rollno, dtype: int64

The dataframe after subtracting 2 from student_rollno, 10 from student_marks columns:
        student_rollno  student_marks
virat               -1             70
nick                 0             25
jessy                1             15
sindhu               2             80

Example4

In a DataFrame, the sub() function can be used to obtain the element-wise subtraction of two series/columns.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random key-value pair(dictionary), index list as arguments to the DataFrame() function of the pandas module to create a dataframe
  • Store it in a variable.
  • Print the given dataframe
  • Subtract the elements of the column student_rollno from the student_marks using the sub() function and store it in as a new column in the dataframe.
  • Print the dataframe after adding a new column(student_marks – student_rollno).
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random key-value pair(dictionary), index list as arguments to the 
# DataFrame() function of the pandas module to create a dataframe
# Store it in a variable.
data_frme = pd.DataFrame({
  "student_rollno": [1, 2, 3, 4],
  "student_marks": [80, 35, 25, 90]},
  index= ["virat", "nick" , "jessy", "sindhu"]
)
# Print the given dataframe
print("The given Dataframe:")
print(data_frme)
print()

# Subtract the elements of the column student_rollno from the student_marks using the sub() 
# function and store it in as a new column in the dataframe.
data_frme['student_marks - student_rollno'] = data_frme['student_marks'].sub(data_frme['student_rollno'])
# Print the dataframe after adding a new column(student_marks - student_rollno)
print("The DataFrame after adding a new column(student_marks - student_rollno):")
print(data_frme)

Output:

The given Dataframe:
        student_rollno  student_marks
virat                1             80
nick                 2             35
jessy                3             25
sindhu               4             90

The DataFrame after adding a new column(student_marks - student_rollno):
        student_rollno  student_marks  student_marks - student_rollno
virat                1             80                              79
nick                 2             35                              33
jessy                3             25                              22
sindhu               4             90                              86