简体   繁体   English

如何检查 5 行的每个值是否连续相同 pandas

[英]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.sizepd.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.

相关问题 如何检查 pandas 列中接下来的 3 个连续行是否具有相同的值? - How to check if next 3 consecutive rows in pandas column have same value? 删除 pandas dataframe 中具有相同值的连续行 - Deleting consecutive rows in a pandas dataframe with the same value 如何检查熊猫行中的连续差异 - how to check consecutive difference in rows in pandas 如何检查连续相同的值和值的计数同时出现 pandas - How to check consecutive same value and count of value occur same time pandas pandas 如何检查以下两行是否具有相同的列值 - pandas how to check if the following 2 rows have the same column value 如何检查日期在 pandas 中是否有连续行? - How do I check if a date has consecutive rows in pandas? 如何在 Pandas 中保留具有相同 ID 和连续值的行? - How to keep rows with same ID and consecutive values in Pandas? 如何获得最大值为 k 连续行的新 pandas dataframe? - How to get new pandas dataframe with max value of k consecutive rows? 如何检查值是否在数据帧或 numpy 数组中的两个连续行之间? - How to check if the value is between two consecutive rows in dataframe or numpy array? 如何使用django为数据库中的列计算具有相同值的连续行? - How to count consecutive rows with same value for a column in a database using django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM