How to add k-means predicted clusters in a column to a dataframe in Python

旧街凉风 提交于 2019-12-03 13:19:31

问题


Have a question about kmeans clustering in python.

So I did the analysis that way:

from sklearn.cluster import KMeans

km = KMeans(n_clusters=12, random_state=1)
new = data._get_numeric_data().dropna(axis=1)
kmeans.fit(new)
predict=km.predict(new)

How can I add the column with cluster results to my first dataframe "data" as an additional column? Thanks!


回答1:


Assuming the column length is as the same as each column in you dataframe df, all you need to do is this:

df['NEW_COLUMN'] = Series(predict, index=df.index)


来源:https://stackoverflow.com/questions/38372188/how-to-add-k-means-predicted-clusters-in-a-column-to-a-dataframe-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!