如何在 pandas 中以10为底的错误修复int()的无效文字 [英] How do I fix invalid literal for int() with base 10 error in pandas

查看:69
本文介绍了如何在 pandas 中以10为底的错误修复int()的无效文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是每当我尝试将数据框转换为int时出现的错误.

This is the error that is showing up whenever i try to convert the dataframe to int.

(以10为底的int()的无效文字:'260,327,021'",发生在人口1"索引上"

df中的所有内容都是数字.我认为该错误是由于末尾有多余的引号引起的,但是我该如何解决?

Everything in the df is a number. I assume the error is due to the extra quote at the end but how do i fix it?

推荐答案

我运行此

int('260,327,021')

得到这个

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-448-a3ba7c4bd4fe> in <module>()
----> 1 int('260,327,021')

ValueError: invalid literal for int() with base 10: '260,327,021'

我向您保证,数据框中的所有内容都不是数字.它可能看起来像一个数字,但是它是一个带有逗号的字符串.

I assure you that not everything in your dataframe is a number. It may look like a number, but it is a string with commas in it.

您将要替换逗号,然后转到 int

You'll want to replace your commas and then turn to an int

pd.Series(['260,327,021']).str.replace(',', '').astype(int)

0    260327021
dtype: int64

这篇关于如何在 pandas 中以10为底的错误修复int()的无效文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆