Add DataFrame and Other

To add DataFrame with scalar, sequence, Series or DataFrame element-wise in Pandas, call add() method on this DataFrame and pass the other (scalar, sequence, Series or DataFrame) object.

In this tutorial, we will learn the syntax of add() method, and how to use this method to add scalar, sequence, Series or other DataFrame to this DataFrame, with examples.

Syntax

The syntax of pandas DataFrame.add() method is

</>
Copy
DataFrame.add(other, axis='columns', level=None, fill_value=None)

where

ParameterValueDescription
otherscalar, sequence, Series, or DataFrameAny single or multiple element data structure, or list-like object.
axis{0 or ‘index’, 1 or ‘columns’}Whether to compare by the index (0 or ‘index’) or columns (1 or ‘columns’). For Series input, axis to match Series index on.
levelint or labelBroadcast across a level, matching Index values on the passed MultiIndex level.
fill_valuefloat or None, default NoneFill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing.

Return Value

DataFrame Result of the arithmetic operation.

Examples

Add DataFrame and Scalar

Example.py

</>
Copy
import pandas as pd

df = pd.DataFrame(
    {'col1': [10, 20, 30], 'col2': [40, 50, 60]})

#scalar
other = 1

result = df.add(other)
print(result)

Output

   col1  col2
0    11    41
1    21    51
2    31    61

Add DataFrame with Sequence

In Python, List, Tuple and Range are sequences. In the following example, we take a DataFrame, and add a List to this DataFrame.

Since the default value of axis is ‘columns’, number of columns of this DataFrame must equal the length of sequence for addition. Therefore only two elements in the sequence is taken.

Example.py

</>
Copy
import pandas as pd

df = pd.DataFrame(
    {'col1': [10, 20, 30], 'col2': [40, 50, 60]})

other = [1, 4]

result = df.add(other)
print(result)

Output

   col1  col2
0    11    44
1    21    54
2    31    64

If addition is done with axis = ‘index’, then the sequence must have three elements which is the number of rows in the DataFrame.

Example.py

</>
Copy
import pandas as pd

df = pd.DataFrame(
    {'col1': [10, 20, 30], 'col2': [40, 50, 60]})

other = [1, 4, 7]

result = df.add(other, axis='index')
print(result)

Output

   col1  col2
0    11    41
1    24    54
2    37    67

Add DataFrame with Series

In the following example, we take a DataFrame and add this DataFrame with Pandas Series object.

Example.py

</>
Copy
import pandas as pd

df = pd.DataFrame(
    {'col1': [10, 20, 30], 'col2': [40, 50, 60]})

other = pd.Series([1, 3], index=['col1', 'col2'])

result = df.add(other)
print(result)

Output

   col1  col2
0    11    43
1    21    53
2    31    63

Add DataFrame with other DataFrame

In the following program, we take two DataFrames and add one DataFrame with another.

Example.py

</>
Copy
import pandas as pd

df_1 = pd.DataFrame(
    {'col1': [10, 20, 30], 'col2': [40, 50, 60]})

df_2 = pd.DataFrame(
    {'col1': [1, 2, 3], 'col2': [4, 5, 6]})

result = df_1.add(df_2)
print(result)

Output

   col1  col2
0    11    44
1    22    55
2    33    66

Conclusion

In this Pandas Tutorial, we learned how to add scalar, sequence, Series or other DataFrame with this DataFrame using pandas DataFrame.add() method.