get_loc keyerror occurred during coding for networkx use

Asked 2 years ago, Updated 2 years ago, 109 views

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

I entered this code, but it was originally ix, not loc, but I changed it to loc because I heard it was gone. But there is a key error error like below, so how should I solve it?

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-19-90fd5b137e7c> in <module>
      1 for connection_id, connection in connections.iterrows():
      2     station1_name = stations.loc[connection['station1']]['name']
----> 3     station2_name = stations.loc[connection['station2']]['name']
      4     graph.add_edge(station1_name, station2_name, time = connection['time'])

~\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, key, 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, key, 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, key, method, 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

python keyerror pandas

2022-09-20 18:03

1 Answers

station2_name = stations.loc[connection['station2']]['name'] This is where the ultimate error is KeyError: 1239.

There is 1239 in connection['station2'], but 1239 in the stations data frame is not an index, so it seems to be causing an error.


2022-09-20 18:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.