networkx 和 graph 上的 python keyerror

问题描述

我在使用 NetworkX 的图形算法方面苦苦挣扎,并且不断收到以下错误消息。我知道之前已经回答过这个问题(Python 和 networkX keyerror),但似乎我有一个不同的问题。我的代码看起来像这样

graph = nx.Graph()
for connection_id,connection in connections.iterrows():
    station1_name = stations.loc[connection['station1']]['name']
    station_2name = stations.loc[connection['station2']]['name']
    graph.add_edge(station1_name,station_2name,time = connection['time'])
    
    

graph.add_edge('Bank',"Monument",time = 1) 

输出一个关键错误


KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self,key,method,tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()

KeyError: 1239.0

During handling of the above exception,another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-25-f7bd5b253a02> in <module>
      2 for connection_id,connection in connections.iterrows():
      3     station1_name = stations.loc[connection['station1']]['name']
----> 4     station_2name = stations.loc[connection['station2']]['name']
      5     graph.add_edge(station1_name,time = connection['time'])
      6 

~\anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self,key)
   1766 
   1767             maybe_callable = com.apply_if_callable(key,self.obj)
-> 1768             return self._getitem_axis(maybe_callable,axis=axis)
   1769 
   1770     def _is_scalar_access(self,key: Tuple):

~\anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self,axis)
   1963         # fall thru to straight lookup
   1964         self._validate_key(key,axis)
-> 1965         return self._get_label(key,axis=axis)
   1966 
   1967 

~\anaconda3\lib\site-packages\pandas\core\indexing.py in _get_label(self,label,axis)
    623             raise IndexingError("no slices here,handle elsewhere")
    624 
--> 625         return self.obj._xs(label,axis=axis)
    626 
    627     def _get_loc(self,key: int,axis: int):

~\anaconda3\lib\site-packages\pandas\core\generic.py in xs(self,axis,level,drop_level)
   3535             loc,new_index = self.index.get_loc_level(key,drop_level=drop_level)
   3536         else:
-> 3537             loc = self.index.get_loc(key)
   3538 
   3539             if isinstance(loc,np.ndarray):

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self,tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key],method=method,tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 1239

我真的不知道我该如何解决这个问题,也许有人可以帮我解决这个问题?非常感谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)