0

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
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
Nickel
  • 580
  • 4
  • 19

1 Answers1

2

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
halfer
  • 19,824
  • 17
  • 99
  • 186
Ch3steR
  • 20,090
  • 4
  • 28
  • 58