indhra lochan
indhra lochan

Reputation: 29

invalid literal for int() with base 10: '100,000'

I ran:

df_new['product_count']=df_new.product_count.astype(int)

And got this error message:

ValueError                                Traceback (most recent call last)
<ipython-input-210-d9dc69e3d064> in <module>()
----> 1 df_new['product_count']=df_new.product_count.astype(str).astype(int)

7 frames
/usr/local/lib/python3.7/dist-packages/pandas/_libs/lib.pyx in pandas._libs.lib.astype_intsafe()

ValueError: invalid literal for int() with base 10: '100,000'

Upvotes: 1

Views: 173

Answers (1)

mozway
mozway

Reputation: 260410

You can't directly convert numbers with thousands separators, you first need to remove them.

Use:

df_new['product_count'] = pd.to_numeric(df_new['product_count'].str.replace(',', ''), errors='coerce')

You can first omit the errors='coerce' option to see if you have errors, and which ones.

Upvotes: 1

Related Questions