简体   繁体   English

pandas dataframe根据列中的值删除后续行

[英]pandas dataframe removing following row based on value in column

simple question but I have not been able to find anything on this. 简单的问题,但我没有找到任何关于此的内容。 I have the following dataframe, and hope to accomplish the following: 我有以下数据框,并希望完成以下内容:

where Roll = 1 in our dataframe, I want to delete the next row, and only that one row. 在我们的数据框中Roll = 1,我想删除下一行,只删除那一行。 Keep in mind that many rows for Roll will be 0 but only a few need to be removed specifically rows where the preceding row for Roll = 1 请记住,Roll的许多行将为0,但只有少数行需要特别删除其中Roll = 1的前一行

Date    Close    Open    Roll
1       500      499     0
2       502      502     0
3       500      499     0
4       500      499     1
5       501      502     0 (delete this row)
6       506      506     0
7       509      508     0

Create a boolean mask using shift that tests whether the previous row contains 1 or not: 使用shift创建一个布尔掩码,测试前一行是否包含1:

In [24]:
df[df['Roll'].shift() != 1]

Out[24]:
   Date  Close  Open  Roll
0     1    500   499     0
1     2    502   502     0
2     3    500   499     0
3     4    500   499     1
5     6    506   506     0
6     7    509   508     0

the boolean mask looks like this: 布尔掩码如下所示:

In [25]:
df['Roll'].shift() != 1

Out[25]:
0     True
1     True
2     True
3     True
4    False
5     True
6     True
Name: Roll, dtype: bool

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM