Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python equivalence of R's match() for indexing

So i essentially want to implement the equivalent of R's match() function in Python, using Pandas dataframes - without using a for-loop.

In R match() returns a vector of the positions of (first) matches of its first argument in its second.

Let's say that I have two df A and B, of which both include the column C. Where

A$C = c('a','b')
B$C = c('c','c','b','b','c','b','a','a')

In R we would get

match(A$C,B$C) = c(7,3)

What is an equivalent method in Python for columns in pandas data frames, that doesn't require looping through the values.

like image 290
leiberl Avatar asked Oct 05 '16 09:10

leiberl


1 Answers

Here is a one liner:

B.reset_index().set_index('c').loc[A.c, 'index'].values

This solution returns the results in the same order as the input A, as match does in R, so it is a better equivalent than @jezrael's answer, because


Full example:

A = pd.DataFrame({'c':['a','b']})
B = pd.DataFrame({'c':['c','c','b','b','c','b','a','a']})

B.reset_index().set_index('c').loc[A.c, 'index'].values
Output array([6, 2])
like image 128
toto_tico Avatar answered Oct 29 '22 00:10

toto_tico