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
You can use
df['MPRN'] = pd.to_numeric(df['MPRN'], errors="coerce").fillna(0).astype('int64')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With