[英]How do I convert integer column values to categorical or string column values dynamically in python?
I have a column that has the values 1,2,3....
I need to change this value to Cluster_1, Cluster_2, Cluster_3...
dynamically.我有一列的值为1,2,3....
我需要将此值更改为Cluster_1, Cluster_2, Cluster_3...
动态。 My original table looks like below, where cluster_predicted
is a column, containing integer value and I need to convert these numbers to cluster_0, cluster_1...
我的原始表如下所示,其中cluster_predicted
是一列,包含整数值,我需要将这些数字转换为cluster_0, cluster_1...
I have tried the below code我试过下面的代码
clustersDf['clusterDfCategorical'] = "Cluster_" + str(clustersDf['clusterDfCategorical'])
But this is giving me a very weird output as shown below.但这给了我一个非常奇怪的输出,如下所示。
import pandas as pd
df = pd.DataFrame()
df['cols']=[1,2,3,4,5]
df['vals']=['one','two','three','four','five']
df['cols'] =df['cols'].astype(str)
df['cols']= 'confuse_'+df['cols']
print(df)
try this , the string conversion is making the issue for you.试试这个,字符串转换为你制造了问题。 One way to convert to string is to use astype转换为字符串的一种方法是使用 astype
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.