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

前端 未结 4 1421
臣服心动
臣服心动 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 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')]
    

提交回复
热议问题