Finding the intersection between two series in Pandas using index

11,665

Pandas indexes have an intersection method which you can use. If you have two Series, s1 and s2, then

s1.index.intersection(s2.index)

or, equivalently:

s1.index & s2.index

gives you the index values which are in both s1 and s2.

You can then use this list of indexes to view the corresponding elements of a series. For example:

>>> ixs = s1.index.intersection(s2.index)
>>> s1.loc[ixs]
# subset of s1 with only the indexes also found in s2 appears here
Share:
11,665
Boss1295
Author by

Boss1295

Updated on July 24, 2022

Comments

  • Boss1295
    Boss1295 almost 2 years

    I have two series of different lengths, and I am attempting to find the intersection of the two series based on the index, where the index is a string. The end result is, hopefully, a series that has the elements of the intersection based on the common string indexes.

    Any ideas?