Convert column float64 to int64 in Pandas

I tried converting a column from a data type float64to int64using:

df['column name'].astype(int64)

but got an error:

NameError: name 'int64' undefined

There are a number of people in the column, but it was formatted as 7500000.0, any idea, how can I just change this float64to int64?

+6
source share
2 answers

I think you need to click on numpy.int64:

df['column name'].astype(np.int64)

Example:

df = pd.DataFrame({'column name':[7500000.0,7500000.0]})
print (df['column name'])
0    7500000.0
1    7500000.0
Name: column name, dtype: float64

df['column name'] = df['column name'].astype(np.int64)
#same as
#df['column name'] = df['column name'].astype(pd.np.int64)
print (df['column name'])
0    7500000
1    7500000
Name: column name, dtype: int64

If some columns NaNin the columns should replace them with int(for example 0) on fillna, because typeof NaNis float:

df = pd.DataFrame({'column name':[7500000.0,np.nan]})

df['column name'] = df['column name'].fillna(0).astype(np.int64)
print (df['column name'])
0    7500000
1          0
Name: column name, dtype: int64

-

EDIT:

NaN :

df = pd.DataFrame({'column name':[7500000.0,np.nan]})

df['column name'] = df['column name'].values.astype(np.int64)
print (df['column name'])
0                7500000
1   -9223372036854775808
Name: column name, dtype: int64
+13

'int64':

>>> import pandas as pd
>>> df = pd.DataFrame({'a': [1.0, 2.0]})  # some test dataframe

>>> df['a'].astype('int64')
0    1
1    2
Name: a, dtype: int64

64- :

>>> df['a'].astype('i8')      # integer with 8 bytes (64 bit)
0    1
1    2
Name: a, dtype: int64

>>> import numpy as np
>>> df['a'].astype(np.int64)  # native numpy 64 bit integer
0    1
1    2
Name: a, dtype: int64

np.int64 ( numpy.array):

>>> np.int64(df['a'])
array([1, 2], dtype=int64)
+1

Source: https://habr.com/ru/post/1017237/


All Articles