Trusted answers to developer questions

What is the Python DataFrame.add() method in Pandas?

Get Started With Machine Learning

Learn the fundamentals of Machine Learning with this free course. Future-proof your career by adding ML skills to your toolkit — or prepare to land a job in AI or Data Science.

Overview

The DataFrame.add() method in Python is used to perform element-wise addition between DataFrame and others Series, constants, and more.. It also provides a facility to fill empty values in DataFrame.

Note: The arithmetic + operator can also be used for addition. However, it does not provide a facility to deal with NaN values and other objects.

Syntax


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

Parameter values

The DataFrame.add() method takes the following argument values:

  • other: This parameter value can either be a DataFrame, array-like, series, or scalar.
  • axis: The default value of this parameter value is 'columns'.
    • It can be 1 or 'columns' (this represents DataFrames).
    • It can be 0 or 'index' (this represents series).
  • level: The default value of this parameter value is None. The broadcast value across a level can be either int or None.
  • fill_value: The default value of this parameter value is None. It can either be a float value or None, both of which help to fill empty NaN fields.

Return value

This method returns a Pandas DataFrame.

Explanation

We can use this function in multiple ways. Here is a list of some scenarios where we can use this function:

  1. When we are working with a DataFrame and a constant
  2. When we are working with two DataFrames
  3. When we are working with a DataFrame and series

In this program, we are going to discuss all the scenarios mentioned above.

# importing Pandas and Numpy
import pandas as pd
import numpy as np
# Creating a DataFrame
df1= pd.DataFrame([[5, 3, 6, 4],
[11, None, 4, 3],
[4, 3, 8, None],
[5, 4, 2, 8]])
# Creating another DataFrame
df2= pd.DataFrame([[None, 4, 5, 9],
[1, None, 4, 3],
[14, 3, -1, None],
[2, 14, 8, 8]])
# Creating Numpy array
data = np.array([1, 2, 3, 4])
# Converting to Python series
series = pd.Series(data)
"""DataFrame and Constant"""
# Invoking add() method
# Evaluating addition between df1 and constant
addition= df1.add(3, fill_value = 2)
# print results
print("DataFrame and Constant")
print(addition)
"""Both are DataFrames"""
# Evaluating addition between df1 and df2
addition= df1.add(df2, fill_value = 2)
print("\nBoth are DataFrames")
print(addition)
"""DataFrame and Series"""
# Evaluating addition between df1 and series
addition= df1.add(series)
print("\nDataFrame and Series")
print(addition)
  • Lines 5–8: We create a DataFrame called df1. It has 16 entries, that is, four rows and four columns.
  • Lines 10–13: We create a DataFrame called df2. It also has four rows and four columns.
  • Line 17: We create an object of Pandas series that contains four entries.
  • Line 22: We invoke the df1.add(3, fill_value=2) function with DataFrame df1 as the self object and the constant value 3 as an argument. This results in element-wise addition. fill_value = 2 replaces the NaN values with 2.
  • Line 29: We invoke the df1.add(df2) function with DataFrame as the self object and DataFrame as an argument. This results in element-wise addition. fill_value = 2 replaces all the NaN values with 2.
  • Line 35: We invoke the df1.add(series) function with DataFrame as the self object and the series object as an argument.

RELATED TAGS

add
pandas
dataframe
python
Did you find this helpful?