简体   繁体   English

如何使用Python从.csv文件中获取行和上一行?

[英]How to grab row and previous row from .csv file using Python?

I'm reading in a.csv file that looks something like this:我正在阅读看起来像这样的.csv 文件:

   DateTime             Failures
0  2020-05-27 00:10:49  0
1  2020-05-27 00:10:49  0
2  2020-05-27 00:21:55  0
3  2020-05-27 00:22:56  1
4  2020-05-27 00:22:59  0

What I'm trying to do is grab any row that has a failure along with the previous row.我想要做的是抓住任何与前一行一起失败的行。

I can get the rows with failures by using我可以通过使用获取失败的行

log_file = pd.read_csv(self.input.text())

failures = log_file[log_file['Failures'] != 0]

but am unsure how to also grab the row before each failure.但我不确定如何在每次失败之前也抓住这一行。

I'm very new to Python and feel like there's probably an easy solution for my problem, I'm just not sure how to get it.我对 Python 很陌生,感觉可能有一个简单的解决方案可以解决我的问题,我只是不知道如何得到它。

Use .shift to also check a condition on the previous row.使用.shift还可以检查上一行的条件。 As the last row becomes NaN we'll fill with 0 so it doesn't accidentally get flagged by that part of the condition.当最后一行变成NaN时,我们将用 0 填充,这样它就不会被条件的那部分意外标记。 ( .ne(0) is the same as != 0 , just a preference) .ne(0)!= 0相同,只是一个偏好)

df[df['Failures'].ne(0) | df['Failures'].shift(-1).fillna(0).ne(0)]

             DateTime  Failures
2 2020-05-27 00:21:55         0
3 2020-05-27 00:22:56         1

.shift(-1) brings all the values to the previous row .shift(-1)将所有值带到上一行

df['Failures'].shift(-1)

0    0.0
1    0.0
2    1.0     # Used to be 1 on row labeled with `3`
3    0.0
4    NaN     # Need to fill with 0 as NaN != 0 evaluates to True

You can use the .index to capture the failure indexes and go from there.您可以使用.index从那里捕获故障索引和 go。

indexes = log_file.index[log_file['Failures'] != 0].tolist()

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

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