The 365 Data Science team is proud to invite you to our own community forum. A very well built system to support your queries, questions and give the chance to show your knowledge and help others in their path of becoming Data Science specialists.
Ask
Anybody can ask a question
Answer
Anybody can answer
Vote
The best answers are voted up and moderated by our team

Customer analytics

Customer analytics

0
Votes
1
Answer

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 Answer

365 Team
0
Votes

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

×
LAST CHANCE
Ready to Learn Data Science?
50% OFF