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

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

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

<pre><code>A$C = c('a','b')
B$C = c('c','c','b','b','c','b','a','a')
</code></pre>

<p>In R we would get</p>

<pre><code>match(A$C,B$C) = c(7,3)
</code></pre>

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

<p>You can also simulate the same behavior using <a href="http://pandas.pydata.org/pandas-docs/stable/comparison_with_r.html#match" rel="nofollow"><code>pd.match</code></a>:</p>

<pre><code>s1 = A['C']
print(s1)

#0    a
#1    b
#Name: C, dtype: object

s2 = B['C']
print(s2)

#0    c
#1    c
#2    b
#3    b
#4    c
#5    b
#6    a
#7    a
#Name: C, dtype: object

s = pd.Series(pd.match(s2, s1, np.nan))
print(s)

#0    NaN
#1    NaN
#2    1.0
#3    1.0
#4    NaN
#5    1.0
#6    0.0
#7    0.0
#dtype: float64
</code></pre>

<p>Then, take unique elements and drop the <code>NaN</code> values and return indices of the values remaining:</p>

<pre><code>arr, idx = np.unique(s, return_index=True)
idx[~np.isnan(arr)]
## array([6, 2], dtype=int64)
</code></pre>

<p>To get the array to be the same as the one obtained in R, simply shift the series by one position downwards to suppress the zero-based indexing problem:</p>

<pre><code>arr, idx = np.unique(s.shift(), return_index=True)
idx[~np.isnan(arr)]
## array([7, 3], dtype=int64)
</code></pre>

python pandas indexing match