Home > Software design >  Select all rows between specific row values in a within columns
Select all rows between specific row values in a within columns

Time:04-07

Im trying to select all the rows of data between rows with these values in E01032739 and E01033708, does anyone know how to do this. Trying to do this so i can count the number of casualties between these area codes.

At the minute i can find all of the data with each set of values but cannot modify the code to get everything in between using this;

 accidents.loc[accidents['LSOA_of_Accident_Location'] == 'E01032739']
 accidents.loc[accidents['LSOA_of_Accident_Location'] == 'E01033708']

Data snippet here if needed;

Accident_Index  Number_of_Casualties    LSOA_of_Accident_Location
97459           34                      E01032739
97461           32                      E01033708
97762           12                      E01033708

CodePudding user response:

This should be concise enough:

accidents.query("'E01032739' <= LSOA_of_Accident_Location <= 'E01033708'")

CodePudding user response:

Is this what you are looking for ?

accidents[(accidents['LSOA_of_Accident_Location'] >= 'E01032739')&(accidents['LSOA_of_Accident_Location'] <= 'E01033708')]

CodePudding user response:

This code does the job:

from_index = list(accidents[accidents["LSOA_of_Accident_Location"] == "E01032739"].index)[0]
to_index = list(accidents[accidents["LSOA_of_Accident_Location"] == "E01033708"].index)[0]

accidents.iloc[from_index: to_index   1, :]

CodePudding user response:

It's hard to say without a dummy datset, but I think this should work:

bottom_idx = accidents[accidents['LSOA_of_Accident_Location'] == 'E01032739'].index.values.astype(int)[0]
upper_idx = accidents[accidents['LSOA_of_Accident_Location'] == 'E01033708'].index.values.astype(int)[0]
accidents.iloc[bottom_idx:upper_idx]
  • Related