Adding of two data frames

The add() method of pandas does an element wise addition of two dataframe objects and returns the resultant dataframe.The python example creates two dataframes and adds their elements which are printed onto the console.

Dataframe Binaryoperatorfunctions Updated 6 Jun 2019 1 min read

Overview

  • The Python library pandas, offers several methods to handle two-dimensional data through the class DataFrame.
  • Two 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.

Example

python
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

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

##