Pandas DataFrame Addition: add() function

The Python library pandas, offers several methods to handle two-dimensional data through the class DataFrameTwo DataFrames can be added by using the add() method of pandas DataFrame class.

Calling add() method is similar to calling the operator +. However, the add() method can be passed a fill value, which will be used for NaN values in the DataFrame.

The pandas addition function performs the addition of dataframes. The addition is performed element-wise. 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

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

  • other: scalar, sequence, Series, or DataFrame – This parameter consists of any single or multiple element data structure or list-like object.
  • axis : {0 or ‘index’, 1 or ‘columns’} – This is used for deciding the axis on which the operation is applied.
  • level : int or label – The level parameter is used for broadcasting across a level and matching Index values on the passed MultiIndex level.
  • fill_value : float or None, default None – Whenever the dataframes have missing values, then to fill existing missing (NaN) values, we can use fill_value parameter.

The function will output the result of the addition function.

Example 1: Addition using pandas add()

In this example, we are adding value to all the elements of the dataframe.

input:

df = pd.DataFrame({'speed': [80, 90, 110],
                        'weight': [250, 200, 150]},
                       index=['Audi', 'Jaguar', 'BMW'])

input:

df

output:

 speedweight
Audi80250
Jaguar90200
BMW110150

Input :

df.add(27)

Output:

 speedweight
Audi107277
Jaguar117227
BMW137177

Example 2: Addition of two dataframes

Here we have created another dataframe, which is then passed to the pandas add() function. We can see in the output that the two dataframes have been added.

Input:

df1 =  pd.DataFrame({'speed': [80, 90, 110],
                     'weight': [250, 200, 150]},
                    index=['Audi', 'Jaguar', 'BMW'])

Input:

df1

Output:

 speedweight
Audi80250
Jaguar90200
BMW110150

Input:

df.add(df1)

Output:

 speedweight
Audi160500
Jaguar180400
BMW220300

Example 3: Scalar addition using dataframe

Here in this example, the addition is performed using scalar value which is added to the existing dataframe.

Input:

df

Output:

 speedweight
Audi80250
Jaguar90200
BMW110150

Input:

df + 25

Output:

 speedweight
Audi105275
Jaguar115225
BMW135175

Example 4: 

input:

import pandas as pd

 

dataSet1 = [(10, 20, 30),

            (40, 50, 60),

            (70, 80, 90)];

             

dataFrame1 = pd.DataFrame(data=dataSet1)

 

dataSet2 = [(5, 15, 25),

            (35, 45, 55),

            (65, 75, 85)];

dataFrame2 = pd.DataFrame(data=dataSet2)

 

print("DataFrame1:");

print(dataFrame1);

 

print("DataFrame2:");

print(dataFrame2);

 

result = dataFrame1.add(dataFrame2);

print("Result of adding two pandas dataframes:");

print(result)

output:

DataFrame1:

    0   1   2

0  10  20  30

1  40  50  60

2  70  80  90

DataFrame2:

    0   1   2

0   5  15  25

1  35  45  55

2  65  75  85

Result of adding two pandas dataframes:

     0    1    2

0   15   35   55

1   75   95  115

2  135  155  175
Tags