Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在数据帧中按给定索引顺序连续求和值_Python_Pandas_Dataframe_Indexing - Fatal编程技术网

Python 在数据帧中按给定索引顺序连续求和值

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

我有一个索引值列表,表示我应该在地图上访问的坐标顺序。每个索引表示(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]+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]]