1

I have two panda series, and would simply like to compare their string values, and returning the strings (and maybe indices too) of the values they have in common e.g. Hannah, Frank and Ernie in the example below::

print(x)
print(y)

0        Anne
1        Beth
2    Caroline
3       David
4       Ernie
5       Frank
6      George
7      Hannah
Name: 0, dtype: object
1      Hannah
2       Frank
3       Ernie
4         NaN
5         NaN 
6         NaN
7         NaN

Doing

x == y 

throws a

ValueError: Can only compare identically-labeled Series objects

as does

x.sort_index(axis=0) == y.sort_index(axis=0)

and

x.reindex_like(y) > y

does something, but not the right thing!

npross
  • 1,756
  • 6
  • 19
  • 38

1 Answers1

3

If need common values only you can use convert first column to set and use intersection:

a = set(x).intersection(y)
print (a)
{'Hannah', 'Frank', 'Ernie'}

And for indices need merge by default inner join with reset_index for convert indices to columns:

df = pd.merge(x.rename('a').reset_index(), y.rename('a').reset_index(), on='a')
print (df)
   index_x       a  index_y
0        4   Ernie        3
1        5   Frank        2
2        7  Hannah        1

Detail:

print (x.rename('a').reset_index())
   index         a
0      0      Anne
1      1      Beth
2      2  Caroline
3      3     David
4      4     Ernie
5      5     Frank
6      6    George
7      7    Hannah

print (y.rename('a').reset_index())
   index       a
0      1  Hannah
1      2   Frank
2      3   Ernie
3      4     NaN
4      5     NaN
5      6     NaN
6      7     NaN
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252