我试图在E01032739和E01033708中具有这些值的行之间选择所有数据行,有人知道如何做到这一点吗。试着这样做,这样我就可以计算出这些区号之间的伤亡人数。
目前,我可以找到每一组值的所有数据,但不能修改代码来获得其间的所有数据;
accidents.loc[accidents['LSOA_of_Accident_Location'] == 'E01032739']
accidents.loc[accidents['LSOA_of_Accident_Location'] == 'E01033708']
此处的数据片段(如果需要(;
Accident_Index Number_of_Casualties LSOA_of_Accident_Location
97459 34 E01032739
97461 32 E01033708
97762 12 E01033708
这应该足够简洁:
accidents.query("'E01032739' <= LSOA_of_Accident_Location <= 'E01033708'")
这就是您想要的吗?
accidents[(accidents['LSOA_of_Accident_Location'] >= 'E01032739')&(accidents['LSOA_of_Accident_Location'] <= 'E01033708')]
此代码完成以下工作:
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, :]
很难说没有伪数据集,但我认为这应该有效:
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]