I tried to convert a column from data type float64
to int64
using:
df['column name'].astype(int64)
but got an error:
NameError: name 'int64' is not defined
The column has number of people but was formatted as 7500000.0
, any idea how I can simply change this float64
into int64
?
This seems to be a little buggy in Pandas 0.23.4?
If there are np.nan values then this will throw an error as expected:
But doesn't change any values from float to int as I would expect if "ignore" is used:
It worked if I first converted np.nan:
Now I can't figure out how to get figure out how to get null values back in place of the zeroes since this will convert everything back to float again:
You can need to pass in the string
'int64'
:There are some alternative ways to specify 64-bit integers:
Or use
np.int64
directly on your column (but it returns anumpy.array
):Solution for pandas 0.24+ for converting numeric with missing values:
I think you need cast to
numpy.int64
:Sample:
If some
NaN
s in columns need replace them to someint
(e.g.0
) byfillna
, becausetype
ofNaN
isfloat
:Also check documentation - missing data casting rules
EDIT:
Convert values with
NaN
s is buggy: