如何在Python中将列中的k均值预测簇添加到数据框 [英] How to add k-means predicted clusters in a column to a dataframe in Python
本文介绍了如何在Python中将列中的k均值预测簇添加到数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对python中的kmeans聚类有疑问.
I 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)
km.fit(new)
predict=km.predict(new)
如何将具有聚类结果的列作为附加列添加到我的第一个数据框"data"中?谢谢!
How can I add the column with cluster results to my first dataframe "data" as an additional column? Thanks!
推荐答案
假设列长度与数据框df
中的每一列相同,则您需要做的是:
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)
这篇关于如何在Python中将列中的k均值预测簇添加到数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文