0

I have a table 'users' with a column 'Internet users' containing numeric values but they're as type object. I need to convert them to int, but:

  • When I try users['Internet users'] = users['Internet users'].astype(int) I get an error "invalid literal for int() with base 10: '1,427,647,786'"

  • When I try pd.to_numeric(users['Internet users'],errors='coerce') it runs but returns all values as null. I need all the numeric values.

  • I've tried users['Internet users'] = int(float(users['Internet users'])) but returns an error cannot convert the series to <class 'float'>

How can I convert this column to int avoiding returning null values?

Random Davis
  • 6,662
  • 4
  • 14
  • 24
  • You can't convert strings with commas to `int`s then apparently. Also you can't use `int()` or `float()` on a list. – Random Davis Jul 14 '22 at 22:54
  • You should include example data with your [mre]. [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Jul 14 '22 at 22:56
  • You might get away with just removing the commas, otherwise I've added two duplicate targets that show 1. how to use locale-based functions to convert values in a dataframe column, and 2. what the relevant function is when converting integers (`locale.atoi`). – Andras Deak -- Слава Україні Jul 14 '22 at 22:58
  • `S = pd.Series(['1,427,647,786']); print(S.str.replace(',','').astype(int))` – wwii Jul 14 '22 at 23:04

0 Answers0