Home > Net >  Selecting a row based on conditions on .csv file
Selecting a row based on conditions on .csv file

Time:01-01

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

CodePudding user response:

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)]
  • Related