Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selecting a row based on conditions on .csv file

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading