consider 2 dataframes df1
, df2
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
df2 = pd.dataframe(np.array([[1, 2], [3, 4]]), list('cd'), list('cd'), dtype=int) df2
it's clear df2
in df1
. how test in general?
assuming dataframes contain 0's
, 1s
only, can use 2d convolution
, if element in convoluted output equal number of elements in df2
-
from scipy.signal import convolve2d out = (convolve2d(df1,df2)==df2.size).any()
for generic case, let me use skimage
module , this smart solution
-
from skimage.util import view_as_windows vieww out = ((vieww(df1.values, df2.shape) == df2.values).all(axis=(2,3))).any()
this template-matching problem , has been discussed , have gotten efficient solutions under post : how can check if 1 two-dimensional numpy array contains specific pattern of values inside it?
. post gives indices of places in df1
df2
located.
Comments
Post a Comment