[英]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.我有两个 dataframe 一个 df1 列 'A' 值对于 5 行是相同的,然后在接下来的五行中更改并再次相同,df2 列 'A' 值是随机的,没有连续的相同值。
i want to use np.where () to give flag if df1 condition detected flag==1 and when df2 condition is detected flag==0如果检测到 df1 条件 flag==1 并且检测到 df2 条件 flag==0,我想使用 np.where () 给出标志
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.这里需要找到一种方法,在 dataframe 中检测到连续值时检测标志 1 和未检测到连续值时检测标志 0。
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
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
您可以使用
pd.Series.shift
并检查相等性和pd.Series.cumsum
,然后将GroupBy.size
与pd.Series.eq
一起使用,最后使用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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.