简体   繁体   中英

TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed

My Code:

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM