5 Best Ways to Convert Float to Integer in a Pandas DataFrame

πŸ’‘ Problem Formulation: In data processing with Python, handling numerical data types efficiently is crucial. Converting a DataFrame’s column from float to integer is a common task, usually necessitated when the floating-point numbers are actually representing discrete quantities and need to be treated accordingly. A typical input could be a column with float values like 3.0, 4.2, 5.6, and the desired output would be their integer equivalents 3, 4, 5.

Method 1: Using the astype() Function

The astype() function in Pandas allows us to easily convert the data type of DataFrame columns. It can be used to transform float columns into integers, effectively truncating the decimal part and keeping only the whole number.

β™₯️ Info: Are you AI curious but you still have to create real impactful projects? Join our official AI builder club on Skool (only $5): SHIP! - One Project Per Month

Here’s an example:

import pandas as pd

# Create a DataFrame with float values
df = pd.DataFrame({'float_column': [1.2, 2.5, 3.8]})

# Convert the float column to integers
df['float_column'] = df['float_column'].astype(int)

print(df)

Output:

   float_column
0             1
1             2
2             3

This code snippet creates a pandas DataFrame with a column of floats and uses the astype() method to cast the float values to integers, omitting the decimal part.

Method 2: Using the round() Function before Conversion

Instead of truncating the decimal part, we might want to round it to the nearest whole number. This can be accomplished by using the round() function before conversion.

Here’s an example:

import pandas as pd

# Create a DataFrame with float values
df = pd.DataFrame({'float_column': [1.2, 2.5, 3.8]})

# Round the values and then convert to integers
df['float_column'] = df['float_column'].round().astype(int)

print(df)

Output:

   float_column
0             1
1             3
2             4

In this example, we round the values of the float column first, and then convert the result to integers, ensuring that the values are rounded rather than simply truncated.

Method 3: Using the numpy.floor() or numpy.ceil() Functions

When we want to ensure that the conversion to integers always rounds down or up, we can use NumPy’s floor() or ceil() functions respectively. These functions provide a way to control the rounding direction during conversion.

Here’s an example:

import pandas as pd
import numpy as np

# Create a DataFrame with float values
df = pd.DataFrame({'float_column': [1.2, 2.5, 3.8]})

# Use floor to round down
df['floored'] = np.floor(df['float_column']).astype(int)

# Use ceil to round up
df['ceiled'] = np.ceil(df['float_column']).astype(int)

print(df)

Output:

   float_column  floored  ceiled
0           1.2        1       2
1           2.5        2       3
2           3.8        3       4

This code demonstrates the use of NumPy’s floor() and ceil() functions to apply different rounding logic before converting float values to integers.

Method 4: Using Integer Division

Python’s integer division operator // can be used within a DataFrame to convert float values to integers by dividing by one and keeping only the integer part of the quotient.

Here’s an example:

import pandas as pd

# Create a DataFrame with float values
df = pd.DataFrame({'float_column': [1.2, 2.5, 3.8]})

# Convert to integers using integer division
df['float_column'] = (df['float_column'] // 1).astype(int)

print(df)

Output:

   float_column
0             1
1             2
2             3

Here, the use of integer division truncates the decimal part and the astype(int) call simply confirms the type conversion, solidifying the effect.

Bonus One-Liner Method 5: Using Lambda Functions

Lambda functions can provide a quick one-liner solution by applying an inline defined function that handles the conversion on each element of the DataFrame’s column.

Here’s an example:

import pandas as pd

# Create a DataFrame with float values
df = pd.DataFrame({'float_column': [1.2, 2.5, 3.8]})

# Convert to integers using a lambda function
df['float_column'] = df['float_column'].apply(lambda x: int(x))

print(df)

Output:

   float_column
0             1
1             2
2             3

This compact code snippet achieves the float-to-integer conversion by passing a lambda function to the apply() method, applying the conversion to each element individually.

Summary/Discussion

  • Method 1: astype(). Simple and direct. Truncates decimal part without regard to its value.
  • Method 2: round() then astype(). Rounds the number before conversion. More cognizant of the numeric value.
  • Method 3: NumPy’s floor() or ceil(). Controlled rounding direction. Requires NumPy and an extra step for conversion.
  • Method 4: Integer division. Efficient for truncating. Same result as astype(int) but may be less understood at a glance.
  • Method 5: Lambda Function. Highly customizable. Potentially slower for large DataFrames as it is applied element-wise.