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

Viewed 103

I'm reading in a .csv file that looks something like this:

   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.

2 Answers

Use .shift to also check a condition on the previous row. As the last row becomes NaN we'll fill with 0 so it doesn't accidentally get flagged by that part of the condition. (.ne(0) is the same as != 0, just a preference)

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

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.

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