Python 在数据帧中按给定索引顺序连续求和值
我有一个索引值列表,表示我应该在地图上访问的坐标顺序。每个索引表示(x,y) 使用这些坐标,我生成了一个距离矩阵。我的目标是找到总的旅行距离 我使用Python 在数据帧中按给定索引顺序连续求和值,python,pandas,dataframe,indexing,Python,Pandas,Dataframe,Indexing,我有一个索引值列表,表示我应该在地图上访问的坐标顺序。每个索引表示(x,y) 使用这些坐标,我生成了一个距离矩阵。我的目标是找到总的旅行距离 我使用iloc实现它,在这里我连续地求和索引值。然而,我需要一个自动化的方法来完成这项工作,因为我有大量的数据要处理。有没有更简单的方法 In[6]: dmatrix.iloc[0][8]+dmatrix.iloc[8][11]+dmatrix.iloc[11][6]+dmatrix.iloc[6][10]+dmatrix.iloc[10][3]+dmat
iloc
实现它,在这里我连续地求和索引值。然而,我需要一个自动化的方法来完成这项工作,因为我有大量的数据要处理。有没有更简单的方法
In[6]: dmatrix.iloc[0][8]+dmatrix.iloc[8][11]+dmatrix.iloc[11][6]+dmatrix.iloc[6][10]+dmatrix.iloc[10][3]+dmatrix.iloc[3][4]+dmatrix.iloc[4][2]+dmatrix.iloc[2][14]+dmatrix.iloc[14][1]+dmatrix.iloc[1][7]+dmatrix.iloc[7][12]+dmatrix.iloc[12][13]+dmatrix.iloc[13][15]+dmatrix.iloc[15][5]+dmatrix.iloc[5][9]+dmatrix.iloc[9][0]
您可以创建循环并遍历索引列表:
sum = 0
for i in range(0, len(yourList)-1):
sum += dmatrix.iloc[yourList[i]][yourList[i+1]]
最好的不客气。别忘了将主题标记为已回答;)
sum = 0
for i in range(0, len(yourList)-1):
sum += dmatrix.iloc[yourList[i]][yourList[i+1]]