pandas 如何将列中的 k 均值预测聚类添加到 Python 中的数据帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38372188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add k-means predicted clusters in a column to a dataframe in Python
提问by Keithx
I have a question about kmeans clustering in python.
我有一个关于 python 中 kmeans 聚类的问题。
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)
How can I add the column with cluster results to my first dataframe "data" as an additional column? Thanks!
如何将带有聚类结果的列作为附加列添加到我的第一个数据框“数据”中?谢谢!
回答by Gal Dreiman
Assuming the column length is as the same as each column in you dataframe df
, all you need to do is this:
假设列长度与 dataframe 中的每一列相同df
,您需要做的就是:
df['NEW_COLUMN'] = pd.Series(predict, index=df.index)