我的数据框看起来像-
state msg value
aa a,b,r .22
bb m,b,r 1.43
cc a,b,q .33
dd h,h,f .25
我希望数据框看起来像-
state msg value text
aa a,b,r .22 a,b,r .22
bb m,b,r 1.43 m,b,r 1.43
cc a,b,q .33 a,b,q .33
dd h,h,f .25 h,h,f .25
我已经完成了-
df.info()
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 state 6925 non-null object
1 msg 6925 non-null object
2 value 6925 non-null object
df['text'] = df['state'].astype(str).str.cat(df['value'], sep=' ')
但是出现此错误-
TypeError: Concatenation requires list-likes containing only strings (or missing values). Offending values found in column mixed.
并且没有丢失或空值。
答案 0 :(得分:2)
您只需要更改从“值”列进行连接的值的类型。串联仅适用于合适的数据类型。在您的代码中,字符串+浮点数将不起作用。 这将帮助您:
df['text'] = df['state'].astype(str).str.cat(df['value'].astype(str), sep=' ')