How to Convert float64 Columns to int64 in Pandas?

Convert Float64 To Int64 Columns

Data is available in various formats and so does its subset – the numerical data! What’s with numbers you ask? Well, there is a lot in fact. We have whole numbers, decimals, integers, complex numbers and so on. At times, the data type available in the input is not exactly what is required for analyzing. So, it is imperative to get in order the format in which the numerical data is available for ease of analysis.

This article sets out to explore the conversion of data type from float64 to int64 in one or more columns of the input dataset through the pandas library. Either of the following techniques can be adopted to serve the purpose.

    So, let us get started with importing the pandas library using the below code.

    import pandas as pd
    

    The following dataframe will be used throughout this article to demonstrate the conversion of float64 columns to int64.

    df = pd.DataFrame({'Emp. ID':[2347, 5678, 8753, 4589, 1617],
                       'Work hrs':[50.5, 45.6, 21.9, 65.4, 58.1],
                      'Salary':[10450.05, 48661.24, 7821.55, 45365.01, 51486.49]})
    

    Let us have a look at the data type used in each of these columns using the print(df.dtypes) function.

    Data Type In Input Dataframe
    Data Type in Input Dataframe

    Method I – Using the astype( ) function

    The astype( ) function is one amongst those that work alongside a dataframe. It has a pretty straightforward syntax in which one ought to specify the column name for which the data type is to be converted which in this case would be from foat64 to int64.

    It is evident from the data types displayed previously that the columns ‘Work hrs’ & ‘Salary’ have the float64 data type. Since 64 seems to be the default base for both float & int data types, let us try using the below code to convert the ‘Salary’ column.

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

    Once the above code is run, let us use the print(df.dtypes) function to have a look at the changes effected.

    Selected Column Converted To Int32
    Selected Column Converted to int32!

    The wonder of wonders, ain’t it?! We assumed that the converted data would also be of the 64 data type, only to see that our assumption was false. What shall we do now? Well, let us tell the pandas library what exactly we want.

    df['Salary'] = df['Salary'].astype('Int64')
    

    Run the above code and we shall again check the converted data type.

    Selected Column Converted Into Int64
    Selected Column Converted Into Int64

    Great! What if one wants to convert the data of the ‘Work hrs’ columns too? The same can also be done in a single attempt using the astype( ) function.

    df = df.astype({"Work hrs":'int64', "Salary":'int64'})
    
    Multiple Columns Converted Into Int64
    Multiple Columns Converted into int64

    It is evident from the above image that both columns selected have been converted into the int64 data type.


    Method II – Using the apply( ) function

    This is an alternate technique for the conversion of the selected column(s) in a dataframe from the existing data type to another. It is to be noted that it also requires some assistance from the numpy library. So, let us import it before getting any further.

    import numpy as np
    

    To convert the ‘Salary’ column from float64 to int64, the following code shall be used.

    df['Salary'] = df['Salary'].apply(np.int64)
    
    Salary Column Converted Into Int64
    Salary Column Converted into int64

    One can only use this method to convert the data type of the columns one after the other.


    Conclusion

    Now that we have reached the end of this article, hope it has elaborated on the different techniques for the conversion of foat64 columns into int64 using the pandas library in Python. Here’s another article that details the usage of the positive( ) function from the numpy library in Python. There are numerous other enjoyable and equally informative articles in AskPython that might be of great help to those who are looking to level up in Python. Audere est facere!


    Reference