Python Pandas DataFrame add() Function

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 add() Function:

The add() function of the Pandas module returns the element-by-element addition of a dataframe and other. It’s the same as dataframe + other, but with support to substitute a fill_value for missing data in one of the inputs.

radd, with the reverse version.

Syntax:

DataFrame.add(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 add() Function in Python

Example1

Here, to add a scalar value to the entire DataFrame, we used the add() 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 add() function to the dataframe by passing some random number to it and print the result.
  • Here it adds 5 to all the elements of the whole 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 add() function to the dataframe by passing some random number
# to it and print the result.
# Here it adds 5 to all the elements of the whole dataframe.
print("The result dataframe after adding 5 to the whole dataframe:")
print(data_frme.add(5))

Output:

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

The result dataframe after adding 5 to the whole dataframe:
        student_rollno  student_marks
virat                6             85
nick                 7             40
jessy                8             30
sindhu               9             95

Example2

Here, the ‘other’ argument can be provided as a list to add different scalar values to 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 add() function to the dataframe by passing list as argument to it which adds corresponding list elements to the dataframe and print the result.
  • ┬áHere it adds 2 to the student_rollno column of the dataframe and 10 to 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 add() function to the dataframe by passing list as argument to it 
# which adds corresponding list elements to the dataframe and print the result.
# Here it adds 2 to the student_rollno column of the dataframe.
# and 10 to the student_marks column of the dataframe.
print("The dataframe after adding 2 to student_rollno, 10 to student_marks columns:")
print(data_frme.add([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 adding 2 to student_rollno, 10 to student_marks columns:
        student_rollno  student_marks
virat                3             90
nick                 4             45
jessy                5             35
sindhu               6            100

Example3

Here, add() 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 add() function to the specified column of dataframe by passing some random number to it and print the result.
  • Here it adds 5 to the student_rollno column of the dataframe.
  • Apply add() function to the specified columns of dataframe which adds corresponding list elements to the dataframe columns and print the result.
  • Here it adds 2 to the student_rollno column of the dataframe and 10 to 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 add() function to the specified column of dataframe by passing some random number
# to it and print the result.
# Here it adds 5 to the student_rollno column of the dataframe.
print("The dataframe after adding 5 to student_rollno column:")
print(data_frme["student_rollno"].add(5))
print()
# Apply add() function to the specified columns of dataframe which adds corresponding 
# list elements to the dataframe columns and print the result.
# Here it adds 2 to the student_rollno column of the dataframe.
# and 10 to the student_marks column of the dataframe.
print("The dataframe after adding 2 to student_rollno, 10 to student_marks columns:")
print(data_frme[["student_rollno", "student_marks"]].add([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 adding 5 to student_rollno column:
virat     6
nick      7
jessy     8
sindhu    9
Name: student_rollno, dtype: int64

The dataframe after adding 2 to student_rollno, 10 to student_marks columns:
        student_rollno  student_marks
virat                3             90
nick                 4             45
jessy                5             35
sindhu               6            100

Example4

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
  • Add the columns student_rollno and student_marks using the add() function and store it in as a new column in a dataframe.
  • Print the dataframe after adding a new column (student_rollno + student_marks)
  • 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()

# Add the columns student_rollno and student_marks using the add() 
# function and store it in as a new column in a dataframe.
data_frme['student_rollno + student_marks'] = data_frme['student_rollno'].add(data_frme['student_marks'])
# Print the dataframe after adding a new column (student_rollno + student_marks)
print("The DataFrame after adding a new column (student_rollno + student_marks):")
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_rollno + student_marks):
        student_rollno  student_marks  student_rollno + student_marks
virat                1             80                              81
nick                 2             35                              37
jessy                3             25                              28
sindhu               4             90                              94