【问题标题】:KeyError: "['Value' 'flag'] not in index"KeyError:“['Value''flag'] 不在索引中”
【发布时间】:2018-07-17 02:41:39
【问题描述】:

我提取了以下代码 并使用 .csv 文件进行处理,以提取 2 个变量:INDICATOR 和 SUBJECT。

#这是变量:LOCATION INDICATOR SUBJECT MEASURE FREQUENCY TIME Value flag

这里是文件 .csv 的链接 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)   

然后它给了我以下错误:

KeyError: "['INDICATOR','SUBJECT'] not in index"

【问题讨论】:

    标签: python python-3.x cluster-analysis unsupervised-learning


    【解决方案1】:

    p[['INDICATOR','SUBJECT']] 不会做你想做的事。它不会选择两列,而是选择名称为数组['INDICATOR','SUBJECT'] 的一列。由于此列不存在,因此您会收到错误消息。

    【讨论】:

      猜你喜欢
      • 2018-04-30
      • 2018-05-16
      • 2020-02-03
      • 2017-11-04
      • 2021-08-23
      • 2016-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多