Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i set my INT values from int32 to int64 using pandas

Tags:

python

pandas

I have a question about pandas to_numeric. I'm trying to cast a Colum from a float to INT, however df.info is stating my Colum is being cast to a int32.

First question, I thought the default INT value using to_numeric is int64? Source

to_numeric(arg, errors='raise', downcast=None) Convert argument to a numeric type.

The default return dtype is float64 or int64 depending on the data supplied. Use the downcast parameter to obtain other dtypes.

Second Question, How do I change it from int32 to int 64 as a MPRN is more the ten numbers and I'm getting negative values in my Dataframe now.

My code example

   import pandas as pd

  df['MPRN'] = pd.to_numeric(df['MPRN'], errors="coerce").fillna(0).astype(int).to_frame()

df.info()

MPRN 387 non-null int32

like image 227
Mizanur Choudhury Avatar asked Oct 19 '25 04:10

Mizanur Choudhury


1 Answers

You can use

df['MPRN'] = pd.to_numeric(df['MPRN'], errors="coerce").fillna(0).astype('int64')
like image 68
Mizanur Choudhury Avatar answered Oct 21 '25 16:10

Mizanur Choudhury



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!