Pandas DataFrame.radd


Pandas DataFrame.radd

The DataFrame.radd method in pandas is used to perform the reverse addition operation between a DataFrame and another object (scalar, Series, or DataFrame). It is particularly useful when the addition is not naturally supported or needs customization.


Syntax

The syntax for DataFrame.radd is:

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

Here, DataFrame refers to the pandas DataFrame on which the reverse addition is performed.


Parameters

ParameterDescription
otherThe object to add to the DataFrame. Can be a scalar, Series, or DataFrame.
axisThe axis to align with. Default is 'columns'.
levelIf the axis is a MultiIndex, this parameter specifies the level to broadcast the addition.
fill_valueSpecifies the value to use when one of the objects is missing data. Default is None.

Returns

A new DataFrame containing the result of the reverse addition operation.


Examples

Reverse Addition with a Scalar

Use radd to add a scalar value to all elements of a DataFrame.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'A': [1, 2, 3],
    'B': [4, 5, 6]
}
df = pd.DataFrame(data)

# Perform reverse addition with a scalar
print("Adding 10 to each element of the DataFrame:")
result = df.radd(10)
print(result)

Output

Adding 10 to each element of the DataFrame:
    A   B
0  11  14
1  12  15
2  13  16

Reverse Addition with Another DataFrame

Use radd to perform reverse addition with another DataFrame.

Python Program

import pandas as pd

# Create two DataFrames
data1 = {
    'A': [1, 2, 3],
    'B': [4, 5, 6]
}
data2 = {
    'A': [10, 20, 30],
    'B': [40, 50, 60]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

# Perform reverse addition with another DataFrame
print("Reverse addition of two DataFrames:")
result = df1.radd(df2)
print(result)

Output

Reverse addition of two DataFrames:
    A   B
0  11  44
1  22  55
2  33  66

Reverse Addition with Missing Values

Handle missing values in one of the objects using the fill_value parameter.

Python Program

import pandas as pd
import numpy as np

# Create two DataFrames
data1 = {
    'A': [1, 2, np.nan],
    'B': [4, np.nan, 6]
}
data2 = {
    'A': [10, 20, 30],
    'B': [40, 50, 60]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)

# Perform reverse addition with a fill_value
print("Reverse addition with fill_value=0:")
result = df1.radd(df2, fill_value=0)
print(result)

Output

Reverse addition with fill_value=0:
      A     B
0  11.0  44.0
1  22.0  50.0
2  30.0  66.0

Reverse Addition with a Series

Perform reverse addition with a Series along a specified axis.

Python Program

import pandas as pd

# Create a DataFrame
data = {
    'A': [1, 2, 3],
    'B': [4, 5, 6]
}
df = pd.DataFrame(data)

# Create a Series
series = pd.Series([10, 20])

# Perform reverse addition along the rows (default axis)
print("Reverse addition of DataFrame and Series:")
result = df.radd(series, axis='columns')
print(result)

Output

Reverse addition of DataFrame and Series:
      A   B
0  11  14
1  12  15
2  13  16

Summary

In this tutorial, we explored the DataFrame.radd method in pandas. Key takeaways include:

  • Using radd to perform reverse addition with scalars, Series, or DataFrames.
  • Handling missing values with the fill_value parameter.
  • Specifying the axis for alignment during the operation.

The DataFrame.radd method is a flexible tool for performing addition operations in pandas when the order of operands matters.


Python Libraries