索引错误:使用iloc时索引器太多



我有一个数据帧,我正试图从中向我的图边缘添加属性。具有mean_travel_time的数据帧,这将是我的边缘的属性

另外,我有一个数据列表,它由作为元组的源节点和目标节点组成,如下所示。[(11602399(,(471005(]

现在,在使用set_edge_attribute添加属性时,我需要将数据放入字典中:{(11602399(:11434.67,(471005(:2286.10,}

我做了这样的事:

data_dict={}#Empty dictionary
for i in data:
data_dict[i] = df1['mean_travel_time'].iloc[i]#adding values

但是,我在说索引器太多时出错了有人能帮我纠正这个错误吗?

请以易于复制的格式提供您的数据:

df = pd.DataFrame({
'index': [1, 9, 12, 18, 26],
'sourceid': [1160, 70, 1190, 620, 1791],
'dstid': [2399, 1005, 4, 103, 1944],
'month': [1] * 5,
'distance': [1434.67, 2286.10, 532.69, 593.20, 779.05]
})

如果您试图遍历边列表(如(1,2((,则需要首先为DataFrame设置索引:

df1.set_index(['sourceid', 'dstid'])

然后您可以访问特定的边缘:

df.set_index(['sourceid', 'dstid']).loc[(1160, 2399)]

或者使用边缘列表:

edges = zip(df['sourceid'], df['dstid'])
df.set_index(['sourceid', 'dstid']).loc[edges]

但你不需要去做任何这些,因为事实上,你可以一次完成你的全部dict:

df.set_index(['sourceid', 'dstid'])['mean_travel_time'].to_dict()

最新更新