简体   繁体   English

如果满足条件,则迭代槽行并获取当前且仅下一行 python

[英]Iterating trough rows and getting the current and only next row if a condition is met python

 df_test = pd.DataFrame({'date':['2022-06-01', '2022-06-02', '2022-06-04', '2022-06-08'], 'sent':['true', 'true', 'false', 'true']}) date sent 0 2022-06-01 true 1 2022-06-02 true 2 2022-06-04 false 3 2022-06-08 true 4 2022-06-09 true 5 2022-06-10 false 6 2022-06-12 true 7 2022-06-14 true

i want to iterate trough this df and when sent == 'false' i want to save this and only the next following rows in a new df.我想遍历这个df,当发送=='false'时,我想保存这个,并且只保存新df中的下一行。

so i want two have a second df like this所以我想要两个像这样的第二个df

 date sent 2 2022-06-04 false 3 2022-06-08 true 5 2022-06-10 false 6 2022-06-12 true

Try:尝试:

sent_false = (df_test['sent'] == 'false')

df_sent_false = df_test[sent_false | sent_false.shift(1)]
print(df_sent_false)

         date   sent
2  2022-06-04  false
3  2022-06-08   true

With resetting the index:重置索引:

df_sent_false.reset_index(drop=True, inplace=True)

         date   sent
0  2022-06-04  false
1  2022-06-08   true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在满足同一行中的初始条件后迭代Pandas行 - Iterating through Pandas rows after an initial condition in the same row is met Dataframe 如果在 dataframe 列上满足条件,那么在 dataframe 中获取这一行和接下来的 3 行? - Dataframe if a condition is met on a dataframe column then get this row and the next 3 rows in a dataframe? PYTHON:连续遍历行,并根据行中的值更改值,直到满足条件 - PYTHON: Continually iterating over row(s) and changing values based on values within the row until condition is met 选择某些行(条件满足),但只选择 Python/Numpy 中的某些列 - Select certain rows (condition met), but only some columns in Python/Numpy Python:如果满足条件则删除行 - Python: drop rows if condition is met python pandas:将值设置为下一行-仅针对某些行(根据条件) - python pandas: set value to next row's - only for certain rows (according to condition) 在各个行上迭代 function 直到满足条件,然后继续下一行 - Iterate a function over individual rows until condition is met, then move on to the next row 比较当前和下一项,如果满足条件,则跳过下一项 - compare current and next item, skip next item if condition is met Python/Selenium - 迭代到下一行 - Python/Selenium - Iterating to next row Python for循环:如果满足条件则继续下一次迭代 - Python for loop: continue to next iteration if condition is met
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM