The following code starts by specifying the pattern you want to match. In your case, this was 0 1 0
. You also specify which coordinate in that pattern you want to correspond to the index you're pulling from column B
. You wanted the middle element which is the 1
coordinate in a 0-based indexing scheme.
From there, we're taking column A
and shifting it with Series.shift()
. By default, this includes NaN
values for missing coordinates. The NaN
won't match with 0
or 1
or any other value of interest, so we can directly compare that shifted column to whatever we're supposed to be matching and get the exact kind of True
or False
values that we want.
In order to match your entire pattern, we need to combine those values with a logical AND. To do that, we reduce each shifted column pairwise with s1 & s2
. This returns a new column which is coordinate-wise the logical AND of the originals.
Finally, we use this boolean result which is a series with as many rows as the original DataFrame df
, and we select from df['B']
using it. This returns a new series with just the values from df['B']
at the intended coordinates.
from functools import reduce
matching_values = (0, 1, 0)
matching_index = 1
df['B'][reduce(
lambda s1, s2: s1 & s2,
(df['A'].shift(i)==v for i, v in zip(
xrange(-matching_index, len(matching_values)-matching_index),
matching_values)))]
If using Python 2.x, you don't need to import reduce()
first, but in Python 3.x the zip()
doesn't build an intermediate list, saving on CPU and RAM resources.
Depending on what you're doing, this could easily be extracted into a function exposing the relevant parameters. Magic strings of A
and B
probably aren't ideal and would be appropriate choices. The matching_values
and matching_index
are other likely candidates.