Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Upgrade Migration And Transformation Tips And Techniques

Authors: Edward Whalen ,Jim Czuprynski

1st Edition

0071846050, 978-0071846059

More Books

Students also viewed these Databases questions

Question

1. Which position would you take?

Answered: 1 week ago