0

Let's say I have a dataframe that looks like this...

xdf = pd.DataFrame({ 'foo': ['1', '2', '3']})
foo
1
2
3

And I want to convert the column to type int. I can do that easily with...

df = df.astype({ 'foo': 'int' })

But if my dataframe looks like this...

df = pd.DataFrame({ 'foo': ['1.0', '2.0', '3.0']})
foo
1.0
2.0
3.0

And I try to convert it from object to int then I get this error

ValueError: invalid literal for int() with base 10: '1.0'

Why doesn't this work? How would I go about converting this to an int properly?

Alec Mather
  • 742
  • 5
  • 20
  • That's how the `int()` function works. You can't convert a string with decimals into an integer directly. Convert it to `float` first, then to `int`. – ddejohn Jun 16 '22 at 23:38

2 Answers2

1

Just do a two step conversion, string to float then float to int.

>>> df.astype({ 'foo': 'float' }).astype({ 'foo': 'int' })
   foo
0    1
1    2
2    3

It works with or without the decimal point.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1

You can use the downcast option of to_numeric method.

df['foo'] = pd.to_numeric(df['foo'], downcast='integer')

Reza Soltani
  • 151
  • 4