Question: KeyError('%s not in index' % objarr[mask]) when trying to apply kmeans to data from excel spreadsheet? Code text (in Python): import pandas as pd from

KeyError('%s not in index' % objarr[mask]) when trying to apply kmeans to data from excel spreadsheet? Code text (in Python):

import pandas as pd from scipy import stats from sklearn.cluster import KMeans import matplotlib.pyplot as plt import seaborn as sns

df = pd.read_excel('/Users/Cassady/Dropbox/ThePrimer/soccer_stats.xlsx',sep=';')

#Make a copy of DF df_tr = df

#Transsform the timeOfDay to dummies ##df_tr = pd.get_dummies(df_tr, columns=['timeOfDay'])

#Standardize clmns = ['Sum of Goals', 'Sum of Direct Free kicks','sum_blocked shots', 'sum_successful long'] df_tr_std = stats.zscore(df_tr[clmns])

#Cluster the data kmeans = KMeans(n_clusters=2, random_state=0).fit(df_tr_std) labels = kmeans.labels_

#Glue back to originaal data df_tr['clusters'] = labels

#Add the column into our list clmns.extend(['clusters'])

#Lets analyze the clusters print (df_tr[clmns].groupby(['clusters']).mean())

error text:

Traceback (most recent call last): File "/Users/Cassady/Kmeans own data.py", line 17, in df_tr_std = stats.zscore(df_tr[clmns]) File "/Users/Cassady/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 1958, in __getitem__ return self._getitem_array(key) File "/Users/Cassady/anaconda3/lib/python3.6/site-packages/pandas/core/frame.py", line 2002, in _getitem_array indexer = self.loc._convert_to_indexer(key, axis=1) File "/Users/Cassady/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py", line 1231, in _convert_to_indexer raise KeyError('%s not in index' % objarr[mask]) KeyError: "['Sum of Direct Free kicks' 'sum_blocked shots' 'sum_successful long balls'] not in index"

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!