Convert multiple float columns to int Python pandas

Python Pandas-type-Conversion

In this post, we are going to learn how to Convert multiple float columns to int Python pandas with code examples and how to convert the entire dataframe column float to int

1. astype() to convert float column to int Pandas


The astype() method allows us to pass datatype explicitly, even we can use Python dictionary to change multiple datatypes at a time, where keys specify the column and values specify the new datatype.

In this example we have convert single dataframe column to float to int by using astype() method

Python Program to convert column float to int

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':[100.5,100.7, 10.78],
    'Subject': ['Math', 'Math', 'Music']
}
 


dfobj = pd.DataFrame(Student_dict)


dfobj['Marks'] = dfobj['Marks'].astype(int)

print ('\n float to int:\n',dfobj)
print ('\n float to int converted dataframe :\n',dfobj.dtypes)

Output

 float to int:
    Name  Marks Subject
0  Jack    100    Math
1  Rack    100    Math
2   Max     10   Music

 float to int converted dataframe :
 Name       object
Marks       int32
Subject    object
dtype: object

2. Convert multiple columns float to int Pandas


In this example, we are converting multiple columns that have float values to int by using the astype(int) method of the Pandas library by passing a dictionary.

We are using a Python dictionary to change multiple columns of datatype Where keys specify the column name and values specify a new datatype.

Python Program to convert column float to int

import pandas as pd
  
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max'],
    'Marks':[100.5,100.7, 10.78],
    'Fee':[10.5,200.70, 105.78],
    'Subject': ['Math', 'Math', 'Music']
}
  
 
 
dfobj = pd.DataFrame(Student_dict)
 
dict_columns_type = {'Marks': int,
                'Fee': int
               }
   
dfobj = dfobj.astype(dict_columns_type)
print('dataframe int to float:\n',dfobj)
 
print(f'\n {dfobj.dtypes}')

Output

dataframe int to float:
    Name  Marks  Fee Subject
0  Jack    100   10    Math
1  Rack    100  200    Math
2   Max     10  105   Music

 Name       object
Marks       int32
Fee         int32
Subject    object
dtype: object

3.Convert multiple dataframe columns nan value float to int


Sometimes we have dataframe columns that contain both NAN and float values. We have to convert float values to int and NAN values to zero.

  • We will use numpy library to specific the NAN values
  • The astype() method to convert float to int

Python Program to convert mutiple nan float to int

import pandas as pd
import numpy as np
  
Student_dict = {
    'Age': [np.nan, 3.6, np.nan],
    'Marks':[100.5,100.7, 10.78],
    'Fee':[np.nan,200.70, np.nan]
    
}
  
 
 
dfobj = pd.DataFrame(Student_dict)

dict_columns_type = {'Age':int,
    'Marks': int,
                'Fee': int
               }

 
dfobj = dfobj.fillna(0).astype(dict_columns_type)


print('dataframe column float to int :\n',dfobj)
 
print(f'\n {dfobj.dtypes}')


Output

dataframe column float to int :
    Age  Marks  Fee
0    0    100    0
1    3    100  200
2    0     10    0

 Age      int32
Marks    int32
Fee      int32
dtype: object

4.Convert entire dataframe to float to int


To convert an entire dataframe columns float to int we just need to call the astype() method by using the dataframe object and specifying the datatype in which we want to convert.

Python Program to convert entire dataframe float to int

import pandas as pd
  
Student_dict = {
    'Age': [2.5, 3.6, 3.7],
    'Marks':[100.5,100.7, 10.78],
    'Fee':[10.5,200.70, 105.78]
    
}
  
 
 
dfobj = pd.DataFrame(Student_dict)
 
dfobj = dfobj.astype(int)


print('dataframe column float to int :\n',dfobj)
 
print(f'\n {dfobj.dtypes}')

Output

dataframe column float to int :
    Age  Marks  Fee
0    2    100   10
1    3    100  200
2    3     10  105

 Age      int32
Marks    int32
Fee      int32
dtype: object

Summary

In this post, we have learned how to convert multiple float columns to int Python pandas, how to convert the entire dataframe column float to int using the astype() method of Python Pandas library.