将字符串转换为 int 时出现无效值错误
Invalid value error during converting string to int
我有一个数据集,并且有一个特征包含字符串中的数字,例如
"153", "45", "13", "345"
我想用 python 将这些值转换为整数,我写了这行代码:
df.column = df.column.astype("int")
但是我收到了这个错误:
invalid literal for int() with base 10: '2,83E+05'
有些值如:
3.89E+05, 2.60E+05, 3,13E+05
如何将其转换为任何数值数据类型?
提前致谢
enter code here
问题不在于科学记数法本身,而在于它们是浮点值并且是科学记数法。我发现这是一种单行解决方案:
df.column.astype('float64').astype('int64')
如果您的字符串值符合欧洲惯例,您还可以添加以下行以输入 pandas-friendly 格式。
df.column = df.apply(lambda x: str(x.column).replace(',','.'), axis=1)
df.column.astype('float64').astype('int64')
我有一个数据集,并且有一个特征包含字符串中的数字,例如
"153", "45", "13", "345"
我想用 python 将这些值转换为整数,我写了这行代码:
df.column = df.column.astype("int")
但是我收到了这个错误:
invalid literal for int() with base 10: '2,83E+05'
有些值如:
3.89E+05, 2.60E+05, 3,13E+05
如何将其转换为任何数值数据类型? 提前致谢
enter code here
问题不在于科学记数法本身,而在于它们是浮点值并且是科学记数法。我发现这是一种单行解决方案:
df.column.astype('float64').astype('int64')
如果您的字符串值符合欧洲惯例,您还可以添加以下行以输入 pandas-friendly 格式。
df.column = df.apply(lambda x: str(x.column).replace(',','.'), axis=1)
df.column.astype('float64').astype('int64')