Pandas: How to find a particular pattern in a dataframe column?

前端 未结 4 1395
臣服心动
臣服心动 2020-12-03 09:23

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

相关标签:
4条回答
  • 2020-12-03 09:43

    The shortest way is finding the index at which the pattern starts. Then you just need to select the three following rows.

    In order to find these indexes, a one-liner is enough:

    indexes=df[(df.ColA==pattern[0])&(df["ColA"].shift(-1)==pattern[1])&(df["ColA"].shift(-2)==pattern[2])].index
    

    Then do as the other answer says to get the subsets that you want.

    0 讨论(0)
  • 2020-12-03 09:45
    for col in df:
        index = df[col][(df[col] == pattern[0]) & (df[col].shift(-1) == pattern[1]) & (df[col].shift(-2) == pattern[2])].index
        if not index.empty: print(index)
    
    0 讨论(0)
  • 2020-12-03 09:57

    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
    
    0 讨论(0)
  • 2020-12-03 10:03

    Using the magic of list comprehensions:

    [df.index[i - len(pattern)] # Get the datetime index 
     for i in range(len(pattern), len(df)) # For each 3 consequent elements 
     if all(df['ColA'][i-len(pattern):i] == pattern)] # If the pattern matched 
    
    # [Timestamp('2017-07-10 00:00:00')]
    
    0 讨论(0)
提交回复
热议问题