How to convert float64 to int64 in python

Solution for pandas 0.24+ for converting numeric with missing values:

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

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

ValueError: Cannot convert non-finite values (NA or inf) to integer

#http://pandas.pydata.org/pandas-docs/stable/user_guide/integer_na.html
df['column name'] = df['column name'].astype('Int64')
print (df['column name'])
0    7500000
1    7500000
2        NaN
Name: column name, dtype: Int64

I think you need cast to numpy.int64:

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

Sample:

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 NaNs in columns need replace them to some int (e.g. 0) by fillna, because type of NaN is 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

Also check documentation - missing data casting rules

EDIT:

Convert values with NaNs is buggy:

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

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Let us see how to convert float to integer in a Pandas DataFrame. We will be using the astype()method to do this. It can also be done using the apply() method.

    Method 1: Using DataFrame.astype() method

    First of all we will create a DataFrame:

    import pandas as pd

    list = [['Anton Yelchin', 36, 75.2, 54280.20], 

            ['Yul Brynner', 38, 74.32, 34280.30], 

            ['Lev Gorn', 31, 70.56, 84280.50],

            ['Alexander Godunov', 34, 80.30, 44280.80], 

            ['Oleg Taktarov', 40, 100.03, 45280.30],

            ['Dmitriy Pevtsov', 33, 72.99, 70280.25], 

            ['Alexander Petrov', 42, 85.84, 25280.75]]

    df = pd.DataFrame(list, columns =['Name', 'Age', 'Weight', 'Salary'])

    display(df)

    Output :

    How to convert float64 to int64 in python

    Example 1 : Converting one column from float to int using DataFrame.astype()

    display(df.dtypes)

    df['Weight'] = df['Weight'].astype(int)

    display(df.dtypes)

    Output :

    How to convert float64 to int64 in python

    Example 2: Converting more than one column from float to int using DataFrame.astype()

    display(df.dtypes)

    df = df.astype({"Weight":'int', "Salary":'int'}) 

    display(df.dtypes)

    Output :

    How to convert float64 to int64 in python

    Method 2: Using DataFrame.apply() method

    First of all we will create a DataFrame.

    import pandas as pd

    list = [[15, 2.5, 100.22], [20, 4.5, 50.21], 

            [25, 5.2, 80.55], [45, 5.8, 48.86], 

            [40, 6.3, 70.99], [41, 6.4, 90.25], 

            [51, 2.3, 111.90]]

    df = pd.DataFrame(list, columns = ['Field_1', 'Field_2', 'Field_3'],

                      index = ['a', 'b', 'c', 'd', 'e', 'f', 'g'])

    display(df)

    Output :

    How to convert float64 to int64 in python

    Example 1: Converting a single column from float to int using DataFrame.apply(np.int64)

    import numpy as np

    display(df.dtypes)

    df['Field_2'] = df['Field_2'].apply(np.int64)

    display(df.dtypes)

    Output :

    How to convert float64 to int64 in python

    Example 2: Converting multiple columns from float to int using DataFrame.apply(np.int64)

    display(df.dtypes)

    df['Field_2'] = df['Field_2'].apply(np.int64)

    df['Field_3'] = df['Field_3'].apply(np.int64)

    display(df.dtypes)

    Output :

    How to convert float64 to int64 in python


    How do I convert an int64 column to a string in Python?

    There are four ways of converting integers to strings in pandas..
    Method 1: map(str) frame['DataFrame Column']= frame['DataFrame Column'].map(str).
    Method 2: apply(str) frame['DataFrame Column']= frame['DataFrame Column'].apply(str).
    Method 3: astype(str) ... .
    Method 4: values.astype(str) ... .
    Output:.

    How do you convert all float columns to int in pandas?

    To convert a column that includes a mixture of float and NaN values to int, first replace NaN values with zero on pandas DataFrame and then use astype() to convert. Use DataFrame. fillna() to replace the NaN values with integer value zero.

    How do you convert float to int in Python?

    A float value can be converted to an int value no larger than the input by using the math. floor() function, whereas it can also be converted to an int value which is the smallest integer greater than the input using math. ceil() function. The math module is to be imported in order to use these methods.

    How do you change the datatype of an object to an int in Python?

    If you already have a numeric data type ( int8 , int16 , int32 , int64 , float16 , float32 , float64 , float128 , and boolean ) you can also use astype() to: convert it to another numeric data type (int to float, float to int, etc.) use it to downcast to a smaller or upcast to a larger byte size.