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?

4
  • You can't convert strings with commas to ints then apparently. Also you can't use int() or float() on a list. Commented Jul 14, 2022 at 22:54
  • You should include example data with your minimal reproducible example. How to make good reproducible pandas examples Commented Jul 14, 2022 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). Commented Jul 14, 2022 at 22:58
  • S = pd.Series(['1,427,647,786']); print(S.str.replace(',','').astype(int)) Commented Jul 14, 2022 at 23:04

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.