首页 > 解决方案 > 将列(1234)中带有空格的数字字符串转换为int

问题描述

我尝试将数字字符串转换1 111为数据框列中的 int: df_ga['Count'] = [float(str(val).replace(' ','').replace(',','.' )) 对于 df_ga['Count'].values 中的 val

但它返回:

ValueError: could not convert string to float: '1\u202f111'

我也试过替换和正则表达式

df_ga['Count'] = df_ga['Count'].replace(' ', '')

df_ga.Count = df_ga.Count.replace('\s+', ' ', regex=True)

他们回来了:

ValueError: Unable to parse string "1 111" at position 134

我试过 coerce pero me da NaN

标签: python-3.xpandascastingspaces

解决方案


推荐阅读