I\'d like to find a particular pattern in a pandas dataframe column, and return the corresponding index values in order to subset the dataframe.
Here\'s a sample dat
Here is a solution:
Check if the pattern was found in any of the columns using rolling. This will give you the last index of the group matching the pattern
matched = df.rolling(len(pattern)).apply(lambda x: all(np.equal(x, pattern)))
matched = matched.sum(axis = 1).astype(bool) #Sum to perform boolean OR
matched
Out[129]:
Dates
2017-07-07 False
2017-07-08 False
2017-07-09 False
2017-07-10 False
2017-07-11 False
2017-07-12 True
2017-07-13 False
2017-07-14 False
2017-07-15 False
2017-07-16 False
dtype: bool
For each match, add the indexes of the complete pattern:
idx_matched = np.where(matched)[0]
subset = [range(match-len(pattern)+1, match+1) for match in idx_matched]
Get all the patterns:
result = pd.concat([df.iloc[subs,:] for subs in subset], axis = 0)
result
Out[128]:
ColA ColB
Dates
2017-07-10 100 91
2017-07-11 90 107
2017-07-12 105 99