Pandas Series.add()
Pandas Series.add() method
In this tutorial, we'll explore the add() method in Pandas Series, which is used to add corresponding elements of two Series.
The syntax of the add() method is:
Series.add(other, level=None, fill_value=None, axis=0)where
| Parameter | Description |
|---|---|
other | The Series or scalar to add. |
level | On which level to broadcast the operation. By default, the operation is done on the index. |
fill_value | Fill missing (NaN) values with this value. If both Series lack a value at the same position, it will be filled with this value. |
axis | Axis along which the Series are added. |
Examples for Pandas Series.add()
1. Adding Two Numeric Series
In this example, we shall take two Series objects, series1 and series2, and add the corresponding elements.
Python Program
import pandas as pd
# Create two numeric Series
series1 = pd.Series([10, 20, 30, 40])
series2 = pd.Series([50, 60, 70, 80])
# Use add() to add corresponding elements
result = series1.add(series2)
# Display the results
print("Series 1:")
print(series1)
print("\nSeries 2:")
print(series2)
print("\nResult of Addition:")
print(result)Output
Series 1:
0 10
1 20
2 30
3 40
dtype: int64
Series 2:
0 50
1 60
2 70
3 80
dtype: int64
Result of Addition:
0 60
1 80
2 100
3 120
dtype: int642. Adding Series with Missing Values
In this example, we shall add the corresponding elements of two Series, where the second Series object series2 does not have as many elements as the first Series object series1.
Python Program
import pandas as pd
# Create two Series with missing values
series1 = pd.Series([10, 20, 30, 40])
series2 = pd.Series([50, 60, 70])
# Use add() to add corresponding elements with a fill value
result = series1.add(series2, fill_value=0)
# Display the results
print("Series 1:")
print(series1)
print("\nSeries 2:")
print(series2)
print("\nResult of Addition with Fill Value:")
print(result)Output
Series 1:
0 10
1 20
2 30
3 40
dtype: int64
Series 2:
0 50
1 60
2 70
dtype: int64
Result of Addition with Fill Value:
0 60.0
1 80.0
2 100.0
3 40.0
dtype: float643. Adding Series with Different Index Levels
In this example, we shall take two Series objects with different index levels, and try at add them on a specific level.
Python Program
import pandas as pd
# Create two Series with different index levels
series1 = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
series2 = pd.Series([40, 50, 60], index=['b', 'c', 'd'])
# Use add() to add corresponding elements with a specified level
result = series1.add(series2, level='b')
# Display the results
print("Series 1:")
print(series1)
print("\nSeries 2:")
print(series2)
print("\nResult of Addition with Specified Level:")
print(result)Output
Series 1:
a 10
b 20
c 30
dtype: int64
Series 2:
b 40
c 50
d 60
dtype: int64
Result of Addition with Specified Level:
a NaN
b 60.0
c 80.0
d NaN
dtype: float64Summary
In this tutorial, we've covered the add() method in Pandas Series, exploring its syntax and providing examples for adding two numeric Series, handling missing values, and dealing with different index levels.