我试图将多列从float转换为数据帧中的字符串类型。
我在下面有一个数据框:
df = pd.DataFrame({'User':['101','102','103','104','105','106'],'CountA':[7,8,9,10,11,12],'CountB':[1,2,3,4,5,6],'CountC':[13,14,15,16,17,18]})
我正在尝试使用for循环将列“ CountA”和“ CountB”转换为字符串:
convert = (df["CountA"],df["CountB"])
col_name = ["CountA","CountB"]
for a in convert:
for b in col_name:
df[b] = a.astype(str)
使用for循环转换后,我的CountA值由CountB值替换,如下所示:
df = pd.DataFrame({'User':['101','CountA':[1,18]})
发生了什么事?