How to Convert a float to an integer in Pandas DataFrame

  1. astype(int) to Convert float to int in Pandas
  2. to_numeric() Method to Convert float to int in Pandas

We will demonstrate methods to convert a float to an integer in a Pandas DataFrame - astype(int) and to_numeric() methods.

First, we create a random array using a numpy library and then convert it into Dataframe.

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(5, 5) * 5)
print(df)

If you run this code you will get the output as following which has values in float.

0 1 2 3 4
00.3024483.5519583.8786602.3803524.741592
14.0541870.9409520.4590584.3148010.524993
22.8917334.9268854.9557732.6263734.144166
31.1276393.1968234.1440201.3506320.401138
41.4235372.0194553.0389450.4366573.823888

astype(int) to Convert float to int in Pandas

To convert float into INT we will use astype(int) method provided by Pandas package. The code is,

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(5, 5) * 5)
print('*********** Random Float DataFrame ************')
print(df)
print('***********************************************')
print('***********************************************')
print('*********** Dataframe Converted into INT ************')
print(df.astype(int))
print('***********************************************')
print('***********************************************')

After running the above codes we will get the following output.

*********** Random Float DataFrame ************
0 1 2 3 4
01.5106181.0945614.1574194.4241954.872719
10.4576804.0029592.6609991.6869160.840159
21.7817783.8129240.5618270.5323280.752800
31.4565142.7749552.7001184.5033544.749377
42.2235204.8592380.4509773.2284442.541648
***********************************************
***********************************************
*********** Dataframe Converted into INT ************
 01234
011444
104210
213000
312244
424032
***********************************************
***********************************************

We can round off the float value to int by usingdf.round(0).astype(int).

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(5, 5) * 5)
print('*********** Random Float DataFrame ************')
print(df)
print('***********************************************')
print('***********************************************')
print('*********** Dataframe Converted into INT ************')
print(df.astype(int))
print('***********************************************')
print('***********************************************')
print('*********** Rounding Float value to INT ************')
print(df.round(0).astype(int))
print('***********************************************')
print('***********************************************')

After running the code we will get the following output.

*********** Random Float DataFrame ************
0 1 2 3 4
03.3944580.0712023.3132054.7858780.705612
11.9603530.8420851.2123320.8183432.637940
24.4078114.2390781.4190271.5263101.043394
33.6542164.6729721.7964393.1684260.734009
40.8487961.0240521.8593190.8443781.747628
***********************************************
***********************************************
*********** Dataframe Converted into INT ************
 01234
030340
110102
244111
334130
401101
***********************************************
***********************************************
*********** Rounding Float value to INT ************
 01234
030351
121113
244121
345231
411212
***********************************************
***********************************************

to_numeric() Method to Convert float to int in Pandas

This method provides functionality to safely convert non-numeric types (e.g. strings) to a suitable numeric type.

s = pd.Series(['1.0', '2', -3])
print(pd.to_numeric(s, downcast='integer'))

After running the code we will get the following output.

01
12
2 -3
dtype: int8

Related Article - Pandas DataFrame

  • How to Get Pandas DataFrame Column Headers as a List
  • How to Delete Pandas DataFrame Column
  • How to Convert DataFrame Column to Datetime in Pandas
  • How to Sort Pandas DataFrame by One Column's Values
  • How to get the aggregate of Pandas group-by and Sum
  • How to convert Python dictionary to Pandas DataFrame
  • How to add header row to a pandas DataFrame
  • How to convert Pandas Dataframe to Numpy array
  • How to convert index of a Pandas Dataframe into a column
  • How to count the NaN occurrences in a column in Pandas Dataframe
  • How to change the order of Pandas DataFrame columns
  • How to add one row to Pandas DataFrame
  • How to delete a row based on column value in Pandas DataFrame
  • How to get a value from a cell of a Pandas DataFrame
  • comments powered by Disqus