我試圖在 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
uj5u.com熱心網友回復:
這應該足夠簡潔:
accidents.query("'E01032739' <= LSOA_of_Accident_Location <= 'E01033708'")
uj5u.com熱心網友回復:
這是你想要的 ?
accidents[(accidents['LSOA_of_Accident_Location'] >= 'E01032739')&(accidents['LSOA_of_Accident_Location'] <= 'E01033708')]
uj5u.com熱心網友回復:
這段代碼完成了這項作業:
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, :]
uj5u.com熱心網友回復:
沒有虛擬資料集很難說,但我認為這應該可行:
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]
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/456650.html
下一篇:基于另一列向列添加值
