Last answered:

09 Apr 2020

Posted on:

09 Apr 2020

0

Customer analytics

x_axis = df_segm_pca_kmeans['components2'] y_axis = df_segm_pca_kmeans['components1'] plt.figure(figsize = (10, 8)) sns.scatterplot(x_axis, y_axis, hue = df_segm_pca_kmeans['Legend'], palette = ['g', 'r', 'c', 'm']) plt.title('Clusters by PCA Components') plt.show()     Error  
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.6/dist-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/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Legend'
During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
2 frames
/usr/local/lib/python3.6/dist-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.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Legend'
SEARCH STACK OVERFLOW
<Figure size 720x576 with 0 Axes>
1 answers ( 0 marked as helpful)
Instructor
Posted on:

09 Apr 2020

0
Hi Ramanjaneyulu,  thanks for reaching out!  The error you get is connected to the 'Legend' and not the code for the plot. So, make sure you include a mapping for the segments, which we add as a Legend column: df_segm_pca_kmeans['Legend'] = df_segm_pca_kmeans['Segment K-means PCA'].map({0:'standard',
                                                                                                                                                1:'career focused',
                                                                                                                                                2:'fewer opportunities',
                                                                                                                                                3:'well-off'}) And make sure this comes before you plot by the Components.    Best,  Eli

Submit an answer