user7289 - 8 months ago 207

Python Question

I have two series s1 and s2 in pandas/python and want to compute the intersection i.e. where all of the values of the series are common.

How would I use the concat function to do this? I have been trying to work it out but have been unable to (I don't want to compute the intersection on the indices of s1 and S2, but on the values).

Thanks in advance.

Answer

Place both series in the python set container. See documentation: http://docs.python.org/2/library/sets.html

then use the set intersection method.

s1.intersection(s2) and then transform back to list if needed.

Just noticed pandas in the tag. Can translate back to that.

```
pd.Series(list(set(s1).intersection(set(s2))))
```

from comments I have changed this to a more pythonic expression, which is shorter and easier to read:

```
Series(list(set(s1) & set(s2)))
```

should to the trick, except if the index data is also important to you

Have added the list(....) to translate the set before going to pd.Series Pandas does not accept a set as direct input for a Series.

Source (Stackoverflow)