KeyError: "['Value' 'flag'] not in index"
KeyError: "['Value' 'flag'] not in index"
I have the following code extracted
And used a .csv file to process, in order to extract 2 variables: INDICATOR and SUBJECT.
# 这是变量:位置指示器主题测量频率时间值标志
这是文件 .csv 的 link
https://ufile.io/2wo1j
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.cluster import KMeans
from sklearn.cluster import AgglomerativeClustering
from sklearn.metrics import silhouette_score , silhouette_samples
from sklearn.metrics import adjusted_rand_score
from sklearn.decomposition import PCA
import warnings # current version of seaborn generates a bunch of warnings that we'll ignore
warnings.filterwarnings("ignore")
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
sns.set(style="white", color_codes=True)
# Carrega dataset dos lirios
health = pd.read_csv("saude.csv")
# informação do Dataset
print("Registos Iniciais:")
print (health.head(2))
print("Registos Finais:")
print(health.tail(2))
p = health.reindex(columns=['LOCATION', 'INDICATOR', 'SUBJECT', 'MEASURE', 'FREQUENT', 'TIME', 'Value', 'flag'])
#print(iris.species.value_counts())
p.dropna(axis=1, how='all', inplace=True)
health_matrix = pd.DataFrame.as_matrix(p[['INDICATOR','SUBJECT']])
for n_clusters in range(2,11):
cluster_model = KMeans(n_clusters=n_clusters, random_state=10)
cluster_labels = cluster_model.fit_predict(health_matrix)
silhouette_avg =
silhouette_score(health_matrix,cluster_labels,metric='euclidean')
adj_rand_score = adjusted_rand_score(health['LOCATION'],cluster_labels)
print("Para o nr de clusters =", n_clusters, "A Média da silhueta é:",
silhouette_avg)
print ("Para o nr de clusters =", n_clusters,
"O rand_score ajustado é:", adj_rand_score)
And then it gives me the following error:
KeyError: "['INDICATOR','SUBJECT'] not in index"
p[['INDICATOR','SUBJECT']]
没有按照您的意愿进行。它不是 select 两列,而是一列,名称是数组 ['INDICATOR','SUBJECT']
。由于此列不存在,您会收到错误消息。
I have the following code extracted And used a .csv file to process, in order to extract 2 variables: INDICATOR and SUBJECT.
# 这是变量:位置指示器主题测量频率时间值标志
这是文件 .csv 的 link https://ufile.io/2wo1j
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.cluster import KMeans
from sklearn.cluster import AgglomerativeClustering
from sklearn.metrics import silhouette_score , silhouette_samples
from sklearn.metrics import adjusted_rand_score
from sklearn.decomposition import PCA
import warnings # current version of seaborn generates a bunch of warnings that we'll ignore
warnings.filterwarnings("ignore")
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
sns.set(style="white", color_codes=True)
# Carrega dataset dos lirios
health = pd.read_csv("saude.csv")
# informação do Dataset
print("Registos Iniciais:")
print (health.head(2))
print("Registos Finais:")
print(health.tail(2))
p = health.reindex(columns=['LOCATION', 'INDICATOR', 'SUBJECT', 'MEASURE', 'FREQUENT', 'TIME', 'Value', 'flag'])
#print(iris.species.value_counts())
p.dropna(axis=1, how='all', inplace=True)
health_matrix = pd.DataFrame.as_matrix(p[['INDICATOR','SUBJECT']])
for n_clusters in range(2,11):
cluster_model = KMeans(n_clusters=n_clusters, random_state=10)
cluster_labels = cluster_model.fit_predict(health_matrix)
silhouette_avg =
silhouette_score(health_matrix,cluster_labels,metric='euclidean')
adj_rand_score = adjusted_rand_score(health['LOCATION'],cluster_labels)
print("Para o nr de clusters =", n_clusters, "A Média da silhueta é:",
silhouette_avg)
print ("Para o nr de clusters =", n_clusters,
"O rand_score ajustado é:", adj_rand_score)
And then it gives me the following error:
KeyError: "['INDICATOR','SUBJECT'] not in index"
p[['INDICATOR','SUBJECT']]
没有按照您的意愿进行。它不是 select 两列,而是一列,名称是数组 ['INDICATOR','SUBJECT']
。由于此列不存在,您会收到错误消息。