TypeError:' DataFrame'对象是可变的,因此它们不能被散列

时间:2017-04-05 18:35:49

标签: python pandas crosstab

我的代码:

samples = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data", sep=',',header=None)
varieties = pd.DataFrame(samples.iloc[:,0])
kmeans = KMeans(n_clusters = 3)
labels = kmeans.fit_predict(samples)
#setting 'labels' according to given data
labels += 1
#converting 'labels' to pandas DataFrame
labels = pd.DataFrame(labels)
df = pd.DataFrame({'labels':[labels], 'varieties':[varieties]})
ct = pd.crosstab(df['labels'],df['varieties'])

我想将这些数据框(标签和品种)用于“交叉号”。功能。 请告诉我如何做到这一点?

1 个答案:

答案 0 :(得分:0)

为什么要将标签存储在单独的数据框中?可能更容易将其保存为variaties数据框中的新列,然后在这两列之间运行交叉表。

samples = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data", sep=',',header=None)
varieties = pd.DataFrame(samples.iloc[:,0])
kmeans = KMeans(n_clusters = 3)
varieties['labels'] = kmeans.fit_predict(samples)
#setting 'labels' according to given data
varieties['labels'] += 1
pd.crosstab(varieties.iloc[:,0], varieties['labels'])