Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finding how many times a sequence repeats in a data frame using python

Is there a way to find how many times a sequence repeats in a dataframe?

Lets say I have a dataframe with a large number of 1 and 3's and I wanted to see how much this sequence [3,1,3,3,1] repeats.

here's an example list. 3,1,3,3,1,3,3,1,3,3,1,3,1,1,1,1,3,1,3,1,1,3,3,3

Here's an example of what I'm trying to do

this first part would be true 3,1,3,3,1,3,3,1,3,3,1,3,1,1,1,1,3,1,3,1,1,3,3,3

this second part would be false 3,1,3,3,1,3,3,1,3,3,1,3,1,1,1,1,3,1,3,1,1,3,3,3

and the third part would be false 3,1,3,3,1,3,3,1,3,3,1,3,1,1,1,1,3,1,3,1,1,3,3,3

I want to analyze sections at a time according to the length of the sequence I'm trying to find. In numeric order of the data frame.

My data Is in a dateandtime format. But I can change that.

Thanks for all your help I really appreciate it everything everybody does on this site.

like image 688
Sam c21 Avatar asked Jun 05 '26 06:06

Sam c21


1 Answers

my_list = np.array([3, 1, 3, 3, 1, 3, 3, 1, 3, 3, 1, 3, 1, 1, 1, 1, 3, 1, 3, 1, 1, 3, 3, 3])
target = np.array([3, 1, 3, 3, 1])
(my_list.reshape(-1, len(sequence)) == sequence[None, :]).all(axis=1)
like image 75
Eelco Hoogendoorn Avatar answered Jun 07 '26 23:06

Eelco Hoogendoorn