Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the intersection between two series in Pandas using index

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?

like image 995
Boss1295 Avatar asked Oct 12 '14 15:10

Boss1295


People also ask

How do you find the intersection of two series in pandas?

# set_one. intersection(set_two, set_three, set_four….) The intersection() function in Python returns a new set containing a common element to all sets. The intersection of two sets is the largest set, including the values common to both sets.

How do you use intersection in pandas?

Intersection of Two data frames in Pandas can be easily calculated by using the pre-defined function merge() . This function takes both the data frames as argument and returns the intersection between them.

What is index intersection?

Index Intersection is a technique built into the SQL Server engine to enable it to use more than one index on a table to satisfy a given query.

Can you index a series in pandas?

Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.


1 Answers

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
like image 73
Alex Riley Avatar answered Oct 26 '22 06:10

Alex Riley