Pandas DataFrame.add
Pandas DataFrame.add
The DataFrame.add method in pandas is used to perform element-wise addition between a DataFrame and another DataFrame, Series, or scalar. It provides the flexibility to handle missing data using the fill_value parameter and allows addition along a specific axis.
Syntax
The syntax for DataFrame.add is:
DataFrame.add(other, axis='columns', level=None, fill_value=None)Here, DataFrame refers to the pandas DataFrame on which the addition operation is performed.
Parameters
| Parameter | Description |
|---|---|
other | Another DataFrame, Series, or scalar to add to the DataFrame. |
axis | Specifies the axis along which the addition is performed. Can be 'columns' (default) or 'index'. |
level | If the DataFrame is a MultiIndex, this specifies the level to match for addition. |
fill_value | Specifies a value to fill in missing data before performing the addition. |
Returns
A new DataFrame resulting from the element-wise addition.
Examples
Adding Two DataFrames
Perform element-wise addition between two DataFrames.
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 element-wise addition
print("Element-wise addition of two DataFrames:")
result = df1.add(df2)
print(result)Output
Element-wise addition of two DataFrames:
A B
0 11 44
1 22 55
2 33 66Adding a Scalar to a DataFrame
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)
# Add a scalar value to all elements
print("Adding 10 to all elements of the DataFrame:")
result = df.add(10)
print(result)Output
Adding 10 to all elements of the DataFrame:
A B
0 11 14
1 12 15
2 13 16Handling Missing Values with fill_value
Use the fill_value parameter to fill missing values before performing addition.
Python Program
import pandas as pd
# Create two DataFrames with missing values
data1 = {
'A': [1, 2, None],
'B': [4, None, 6]
}
data2 = {
'A': [10, 20, 30],
'B': [40, 50, None]
}
df1 = pd.DataFrame(data1)
df2 = pd.DataFrame(data2)
# Add with fill_value
print("Adding two DataFrames with fill_value=0:")
result = df1.add(df2, fill_value=0)
print(result)Output
Adding two DataFrames with fill_value=0:
A B
0 11.0 44.0
1 22.0 50.0
2 30.0 6.0Adding Along a Specific Axis
Perform addition along a specific axis (rows or columns).
Python Program
import pandas as pd
# Create a DataFrame and a Series
data = {
'A': [1, 2, 3],
'B': [4, 5, 6]
}
series = pd.Series([10, 20], index=['A', 'B'])
df = pd.DataFrame(data)
# Add along rows (axis=1)
print("Adding Series along rows (axis=1):")
result = df.add(series, axis=1)
print(result)Output
Adding Series along rows (axis=1):
A B
0 11.0 14.0
1 12.0 15.0
2 13.0 16.0Summary
In this tutorial, we explored the DataFrame.add method in pandas. Key takeaways include:
- Performing element-wise addition between DataFrames, Series, or scalars.
- Using
fill_valueto handle missing values. - Specifying an axis for addition with Series or MultiIndex DataFrames.
The DataFrame.add method is a versatile tool for arithmetic operations in pandas.