Add the Values of a Pandas Series
Let's learn how to add the values of two pandas series.
We'll cover the following
Try it yourself
Try executing the code below to see the result.
import pandas as pdgrades = pd.Series([61, 82, 57])bonuses = pd.Series([10, 5, 10, 10])out = grades + bonusesprint(out)
Explanation
The pandas.Series
and numpy.ndarray
are different from Python lists. The +
operator included on Python lists does concatenation:
In [1]: [1, 2, 3] + [4, 5]
Out[1]: [1, 2, 3, 4, 5]
The numpy.ndarray
and pandas.Series
that is built on it, have different behavior. They’ll do element-wise operations and try to match the dimensions as much as possible, which is a process known as broadcasting.
In [2]: np.array([1,2,3]) + np.array([4,5,6])
Out[2]: array([5, 7, 9])
In [3]: np.array([1,2,3]) + 3
Out[3]: array([4, 5, 6])
If NumPy can’t broadcast, it’ll raise an error like the one below:
In [4]: np.array([1,2,3]) + np.array([4,5,6,7])
...
ValueError: operands could not be broadcast together with shapes (3,) (4,)
This is where pandas diverges from NumPy. The pandas.Series
(and pandas.DataFrame
)
uses labels for matching elements somewhat like a SQL join.
In [5]: s1 = pd.Series([1,2,3], index=['a', 'b', 'c'])
In [6]: s2 = pd.Series([10,20,30], index=['c', 'b', 'a'])
In [7]: s1 + s2
Out[7]:
a 31
b 22
c 13
dtype: int64
When pandas can’t find a matching label, it’ll use nan
for a value, which is what happens in this teaser.
Get hands-on with 1300+ tech skills courses.