Python | Pandas dataframe.add()
Last Updated :
19 Feb, 2021
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Dataframe.add()
method is used for addition of dataframe and other, element-wise (binary operator add). Equivalent to dataframe + other, but with support to substitute a fill_value for missing data in one of the inputs.
Syntax: DataFrame.add(other, axis='columns', level=None, fill_value=None)
Parameters:
other :Series, DataFrame, or constant
axis :{0, 1, ‘index’, ‘columns’} For Series input, axis to match Series index on
fill_value : [None or float value, default None] Fill missing (NaN) values with this value. If both DataFrame locations are missing, the result will be missing.
level : [int or name] Broadcast across a level, matching Index values on the passed MultiIndex level
Returns: result DataFrame
Python3 1==
# Importing Pandas as pd
import pandas as pd
# Importing numpy as np
import numpy as np
# Creating a dataframe
# Setting the seed value to re-generate the result.
np.random.seed(25)
df = pd.DataFrame(np.random.rand(10, 3), columns =['A', 'B', 'C'])
# np.random.rand(10, 3) has generated a
# random 2-Dimensional array of shape 10 * 3
# which is then converted to a dataframe
df
Note: add()
function is similar to '+' operation but,
add()
provides additional support for missing values in one of the inputs.
Python3 1==
# We want NaN values in dataframe.
# so let's fill the last row with NaN value
df.iloc[-1] = np.nan
df
Adding a constant value to the dataframe using add()
function:
Python3 1==
# add 1 to all the elements
# of the data frame
df.add(1)

Notice the output above, no addition took place for the nan cells in the
df dataframe.add()
function has an attribute
fill_value
. This will fill the missing value(Nan) with the assigned value. If both dataframe values are missing then, the result will be missing.
Let's see how to do it.
Python3 1==
# We have given a default value
# of '10' for all the nan cells
df.add(1, fill_value = 10)

All the
nan cells has been filled with 10 first and then 1 is added to it.
Adding Series to Dataframe:
For Series input, the dimension of the indexes must match for both data frame and series.
Python3 1==
# Create a Series of 10 values
tk = pd.Series(np.ones(10))
# tk is a Series of 10 elements
# all filled with 1
Python3 1==
# Add tk(series) to the df(dataframe)
# along the index axis
df.add(tk, axis ='index')
Adding one data frame with other data frame
Python3 1==
# Create a second dataframe
# First set the seed to regenerate the result
np.random.seed(10)
# Create a 5 * 5 dataframe
df2 = pd.DataFrame(np.random.rand(5, 5), columns =['A', 'B', 'C', 'D', 'E'])
df2

Let's perform element-wise addition of these two data frames
Python3 1==

Notice the resulting dataframe has dimension 10*5 and it has
nan value in all those cells for which either of the dataframe has
nan value.
Let's fix it -
Python3 1==
# Set a default value of 10 for nan cells
# nan value won't be filled for those cells
# in which both data frames has nan value
df.add(df2, fill_value = 10)
Similar Reads
Python - Pandas dataframe.append() Pandas append function is used to add rows of other dataframes to end of existing dataframe, returning a new dataframe object. Columns not in the original data frames are added as new columns and the new cells are populated with NaN value.Append Dataframe into another DataframeIn this example, we ar
4 min read
Python | Pandas dataframe.applymap() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Dataframe.applymap() method applies a function that accepts and returns a scalar to ev
2 min read
Python | Pandas Dataframe.pop() The pop() method in Pandas is used to remove a column from a DataFrame and return it as a Series. This is similar in concept to the dictionary pop() method in Python, but specifically designed for use with Pandas DataFrames. It's key features include:Removes a specified column from a DataFrame.Retur
2 min read
Python | Pandas dataframe.eval() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.eval() function is used to evaluate an expression in the context of t
2 min read
Python | Pandas dataframe.radd() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.Pandas dataframe.radd() function performs the addition of the dataframe and other objec
2 min read
Python | Pandas dataframe.aggregate() Dataframe.aggregate() function is used to apply some aggregation across one or more columns. Aggregate using callable, string, dict or list of string/callables. The most frequently used aggregations are:sum: Return the sum of the values for the requested axismin: Return the minimum of the values for
2 min read
Python | Pandas dataframe.insert() Pandas insert method allows the user to insert a column in a data frame or series(1-D Data frame). A column can also be inserted manually in a data frame by the following method, but there isn't much freedom here. For example, even column location can't be decided and hence the inserted column is al
8 min read
Python | Pandas DataFrame.values Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Arithmetic operations align on both row and column labels. It can be thought of as a dict-like container for Series objects. This is the primary data structure o
2 min read
Dataframe Attributes in Python Pandas In this article, we will discuss the different attributes of a dataframe. Attributes are the properties of a DataFrame that can be used to fetch data or any information related to a particular dataframe. The syntax of writing an attribute is: DataFrame_name.attribute These are the attributes of the
11 min read
Python | Pandas DataFrame.transform Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Arithmetic operations align on both row and column labels. It can be thought of as a dict-like container for Series objects. This is the primary data structure o
3 min read