PV8
PV8

Reputation: 6270

Convert object column to int column in python

I somehow have a strange conversion problem in python. I have an object column which I want to convert to an int column to save some memory space.

I run the following command:

df['Number'] = df['IntC'].astype(int)

and somehow for some rows it ends up like this:

IntC               Number
233004885002       -188830000475
233048850003       -195883838200

What is happening here? I would expect this output:

IntC               Number
233004885002       233004885002 
233048850003       233048850003

Upvotes: 0

Views: 464

Answers (2)

somesh kovi
somesh kovi

Reputation: 24

Using Lambda function is a much-generalized option

df['Number'] = df.IntC.apply(lambda c: int(c))

Upvotes: 0

BENY
BENY

Reputation: 323306

If convert object to int , why not do factor

df['Number'] = df.IntC.factorize()[0]

Upvotes: 1

Related Questions