Nickel
Nickel

Reputation: 590

How to check each value consecutive same for 5 rows pandas

I have two dataframe one df1 column 'A' values are same for 5 rows and then change and again same for next five rows, df2 column 'A' values are random no consecutive same value.

i want to use np.where () to give flag if df1 condition detected flag==1 and when df2 condition is detected flag==0

here need to find a way to detect flag 1 when consecutive values detected and detect flag 0 when consecutive values not detected in dataframe.

df1=pd.DataFrame({'A':[1,1,1,1,8,8,8,8,8,15,15,15]})-------> flag==1
df2=pd.DataFrame({'A':[1,3,4,7,8,11,1,15,20,15,16,87]})-----flag==0

Upvotes: 0

Views: 1052

Answers (1)

Ch3steR
Ch3steR

Reputation: 20669

You can use pd.Series.shift and check for equality and pd.Series.cumsum, then use GroupBy.size with pd.Series.eq and finally use pd.Series.any

g = df1['A'].ne(df1['A'].shift()).cumsum()
flag = df1.groupby(g).size().eq(5).any()# if you want consider consecutive elements
# True                                  # greater than equal to 5 replace `.eq` with `.ge`

g1 = df2['A'].ne(df2['A'].shift()).cumsum()
flag2 = df2.groupby(g1).size().eq(5).any()
# False

Upvotes: 2

Related Questions