1

I am trying to select a row based on a certain condition from a .csv file.

enter image description here

As you can see in the image. I have a .csv file containing that table. At the last row the "raw_flow_rate" and "avg_flow_rate" are same. And I am selecting that row with this line of code.

select_data = New_data[New_data["raw_flow_rate"] == New_data["avg_flow_rate"]]

And it works perfectly. But my target is to select the previous row of the selected one. Index number 4 in this case. I have tried the ID column to simply select the previous row based on (ID-1) operation.

storeID = select_data["ID"] - 1
final_data = New_data[New16_data["ID"] == storeID]

But this gives the error can only compare identically-labeled series objects

Is there any simple way to select the previous row from the one which met the condition? I'm using Pandas to handle the .csv files. The files are quite big, so a simpler method is preferable. Thanks

Tousif Zaman
  • 57
  • 4
  • 14
  • Will you please show a sample of the actual CSV file, for copy/pasting? –  Jan 01 '22 at 01:13
  • Sure, there you go [link](https://drive.google.com/file/d/1UGOGW4Vx6YuhJlgLe81GJ7TsDR2GEDfW/view?usp=sharing) – Tousif Zaman Jan 01 '22 at 01:16
  • have you tried using `.shift()`? https://stackoverflow.com/questions/41399538/comparing-previous-row-values-in-pandas-dataframe – ekrall Jan 01 '22 at 01:19

1 Answers1

2

Since New_data["raw_flow_rate"] == New_data["avg_flow_rate"] is a mask, you can shift the mask up by one:

select_data = New_data[(New_data["raw_flow_rate"] == New_data["avg_flow_rate"]).shift(-1).fillna(False)]