Another way is to use Pandas:
import pandas as pd df1 = pd.DataFrame({'x':[1,2.3,3.2,3.3,5,6,7,8,6.2,7]}, dtype=float) df2 = pd.DataFrame({'y': list(range(100))}, dtype=float) s = df2.y.isin(df1.x) pd.np.hstack([df2[s].values, df2[s.shift(1, fill_value=False)].values, df2[s.shift(2, fill_value=False)].values])
Output:array([[ 1., 2., 3.],
[ 5., 6., 7.],
[ 6., 7., 8.],
[ 7., 8., 9.],
[ 8., 9., 10.]])
Note: you need explicitly declare data types for dataframes if you are planning to use .isin
method. .isin
performs some data type casting (see bug) that can lead to unexpected results, e.g. when comparing arrays of float and integer values. This bug is still actual in Pandas v0.25.0.