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?
# 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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With