How to check if 2-D array is in another 2-D array
<p>consider the two dataframes <code>df1</code> and <code>df2</code></p>

<pre><code>df1 = pd.DataFrame(np.zeros((6, 6)), list('abcdef'), list('abcdef'), dtype=int)
df1.iloc[2:4, 2:4] = np.array([[1, 2], [3, 4]])

df1
</code></pre>

<p><a href="http://i.stack.imgur.com/UEWbP.png" rel="nofollow"><img src="http://i.stack.imgur.com/UEWbP.png" alt="enter image description here"></a></p>

<pre><code>df2 = pd.DataFrame(np.array([[1, 2], [3, 4]]), list('CD'), list('CD'), dtype=int)

df2
</code></pre>

<p><a href="http://i.stack.imgur.com/dUPSc.png" rel="nofollow"><img src="http://i.stack.imgur.com/dUPSc.png" alt="enter image description here"></a></p>

<p>It's clear that <code>df2</code> is in <code>df1</code>.  How do I test for this in general?</p>

<h3>Brute force</h3>

<pre><code>def isin2d(small_df, large_df):
    di, dj = small_df.shape
    mi, mj = large_df.shape
    for i in range(mi - di + 1):
        for j in range(mj - dj + 1):
            if (small_df.values == large_df.values[i:i + di, j:j + dj]).all():
                return True

    return False

isin2d(df2, df1)

True
</code></pre>

python pandas numpy scipy