numpy.concatenate() 报错 Keyerror: 0

numpy.concatenate()

官方文档链接

函数说明:

Join a sequence of arrays along an existing axis.
沿现有轴连接阵列序列。


程序报错:

KeyError Traceback (most recent call last)
F:\Anacondaaa\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3079 try:
-> 3080 return self._engine.get_loc(casted_key)
3081 except KeyError as err:

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: 0

The above exception was the direct cause of the following exception:

KeyError Traceback (most recent call last)
in
----> 1 np.unique(np.concatenate(movie_type[‘类型’].map(lambda x: x.split(’/’))))

<array_function internals> in concatenate(*args, **kwargs)

F:\Anacondaaa\lib\site-packages\pandas\core\series.py in getitem(self, key)
851
852 elif key_is_scalar:
–> 853 return self._get_value(key)
854
855 if is_hashable(key):

F:\Anacondaaa\lib\site-packages\pandas\core\series.py in _get_value(self, label, takeable)
959
960 # Similar to Index.get_value, but we do not fall back to positional
–> 961 loc = self.index.get_loc(label)
962 return self.index._get_values_for_loc(self, loc, label)
963

F:\Anacondaaa\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3080 return self._engine.get_loc(casted_key)
3081 except KeyError as err:
-> 3082 raise KeyError(key) from err
3083
3084 if tolerance is not None:

KeyError: 0


原因及建议

keyerror 就是没这条数据的意思,因为 np.concatenate 本身是以原有的索引进行连接的,此时没有成功是因为在数据清洗的过程中原索引不再连续。
重置连续的索引、或使用其他能使列表连接的方法即可实现此效果。

你可能感兴趣的:(Numpy,python)