TypeError:"DataFrame"对象是可变的,因此无法对其进行哈希处理 [英] TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed

查看:112
本文介绍了TypeError:"DataFrame"对象是可变的,因此无法对其进行哈希处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

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'])

我想将这些数据框(标签和品种)用于交叉表"功能. 请让我知道我该怎么做?

I want to use these dataframes (labels and varieties) for 'crosstab' function. Please do let me know how I can do that?

推荐答案

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

Why are you storing the labels in a separate dataframe? Might be easier to save it just as a new column in the variaties dataframe, and then run crosstab between those two columns.

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'])

这篇关于TypeError:"DataFrame"对象是可变的,因此无法对其进行哈希处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆