我收到错误消息ValueError:无法插入群集标签,已存在。我希望有人能帮我一把

2024-06-01 00:41:27 发布

您现在位置:Python中文网/ 问答频道 /正文

# add clustering labels
neighborhoods_venues_sorted.insert(0, 'Cluster_Label', kmeans.labels_)

mia_merged = df_mia

# merge mia_grouped with mia_data to add latitude/longitude for each neighborhood
mia_merged = mia_merged.join(neighborhoods_venues_sorted.set_index('Neighborhood'), on='Neighborhood')

mia_merged.head() # check the last columns!
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-52-fcf52ada735d> in <module>
      1 # add clustering labels
----> 2 neighborhoods_venues_sorted.insert(0, 'Cluster_Label', kmeans.labels_)
      3 
      4 mia_merged = df_mia
      5 

~\Anaconda3\lib\site-packages\pandas\core\frame.py in insert(self, loc, column, value, allow_duplicates)
   3574         self._ensure_valid_index(value)
   3575         value = self._sanitize_column(column, value, broadcast=False)
-> 3576         self._data.insert(loc, column, value, allow_duplicates=allow_duplicates)
   3577 
   3578     def assign(self, **kwargs):







~\Anaconda3\lib\site-packages\pandas\core\internals\managers.py in insert(self, loc, item, value, allow_duplicates)
   1171         if not allow_duplicates and item in self.items:
   1172             # Should this be a different kind of error??
-> 1173             raise ValueError("cannot insert {}, already exists".format(item))
   1174 
   1175         if not isinstance(loc, int):

ValueError: cannot insert Cluster_Label, already exists

我已经在主题行中添加了一些关于我得到的代码错误的解释,似乎Cluster_标签有问题,因为我已经多次将其更改为不同的名称,如ClusterLabels、Cluster Labels等


Tags: inselfaddlabelsvaluecolumnmergedloc