December 2018
Beginner to intermediate
682 pages
18h 1m
English
An exception to the preceding example takes place when the indexes contain the same exact elements in the same order. When this occurs, a Cartesian product does not take place, and the indexes instead align by their position. Notice here that each element aligned exactly by position and that the data type remained an integer:
>>> s1 = pd.Series(index=list('aaabb'), data=np.arange(5))>>> s2 = pd.Series(index=list('aaabb'), data=np.arange(5))>>> s1 + s2a 0
a 2
a 4
b 6
b 8
dtype: int64
If the elements of the index are identical, but the order is different between the Series, a Cartesian product occurs. Let's change the order of the index in s2 and rerun the same operation:
>>> s1 = pd.Series(index=list('aaabb'), data=np.arange(5)) ...