How to Convert Float to Int in Pandas

While I was working on a data analysis project for a US retail dataset, I needed to convert price columns from float to integer format. The challenge was maintaining data accuracy while removing decimal places.

In this article, I will share five practical methods to convert float values to integers in Pandas, based on my decade of experience working with Python data manipulation.

Let’s get into the solutions!

Convert Float to Int in Pandas

Let me show you how to convert a float to an int in Python Pandas.

Method 1 – Use astype() for Simple Conversion

The simplest way to convert float values to integers in Pandas is to use the astype() method in Python. This method truncates the decimal part of the float number (removes everything after the decimal point without rounding).

Let’s see this in action with a sample DataFrame of product prices:

import pandas as pd

# Create a sample DataFrame with float values
data = {'Product': ['Laptop', 'Phone', 'Tablet', 'Monitor', 'Keyboard'],
        'Price': [999.99, 699.50, 349.75, 249.25, 89.99]}

df = pd.DataFrame(data)
print("Original DataFrame:")
print(df)

# Convert float to int using astype()
df['Price_Int'] = df['Price'].astype(int)
print("\nDataFrame after conversion:")
print(df)

Output:

Original DataFrame:
    Product   Price
0   Laptop   999.99
1   Phone    699.50
2   Tablet   349.75
3   Monitor  249.25
4   Keyboard  89.99

DataFrame after conversion:
    Product   Price  Price_Int
0   Laptop   999.99       999
1   Phone    699.50       699
2   Tablet   349.75       349
3   Monitor  249.25       249
4   Keyboard  89.99        89

I executed the above example code and added the screenshot below.

python float to int

Notice that the decimal parts are completely truncated, not rounded. For example, 999.99 becomes 999, not 1000.

Method 2 – Use round() with astype() for Rounding Values

If you want to round the values properly before converting to integers, you can combine the round() function with astype()methods in Python:

import pandas as pd

# Create a sample DataFrame with float values
data = {'Product': ['Laptop', 'Phone', 'Tablet', 'Monitor', 'Keyboard'],
        'Price': [999.99, 699.50, 349.75, 249.25, 89.99]}

df = pd.DataFrame(data)

# Round and then convert to int
df['Price_Rounded_Int'] = df['Price'].round().astype(int)
print("DataFrame with rounded integers:")
print(df)

Output:

DataFrame with rounded integers:
    Product   Price  Price_Rounded_Int
0   Laptop   999.99              1000
1   Phone    699.50               700
2   Tablet   349.75               350
3   Monitor  249.25               249
4   Keyboard  89.99                90

I executed the above example code and added the screenshot below.

float to int python

This method is better when you need to maintain the relative accuracy of your data. For instance, in pricing analyses, rounding might be more appropriate than truncation.

Method 3 – Use apply() with Custom Logic

Sometimes you need more control over the conversion process. Python apply() method allows you to apply custom logic to each value:

import pandas as pd

# Create a sample DataFrame with float values
data = {'Product': ['Laptop', 'Phone', 'Tablet', 'Monitor', 'Keyboard'],
        'Price': [999.99, 699.50, 349.75, 249.25, 89.99],
        'Discount': [0.15, 0.10, 0.20, 0.05, 0.25]}

df = pd.DataFrame(data)

# Custom function to convert float to int with specific logic
def custom_conversion(x):
    # If value is less than 1, multiply by 100 to get percentage
    if x < 1:
        return int(x * 100)
    else:
        return int(x)

# Apply custom conversion to different columns
df['Price_Int'] = df['Price'].apply(custom_conversion)
df['Discount_Percent'] = df['Discount'].apply(custom_conversion)

print(df)

Output:

    Product   Price  Discount  Price_Int  Discount_Percent
0   Laptop   999.99      0.15       999                15
1   Phone    699.50      0.10       699                10
2   Tablet   349.75      0.20       349                20
3   Monitor  249.25      0.05       249                 5
4   Keyboard  89.99      0.25        89                25

I executed the above example code and added the screenshot below.

python convert float to int

This approach is particularly useful when dealing with diverse datasets where different columns need different conversion logic.

Method 4 – Use numpy.floor() or numpy.ceil()

If you need to always round down or always round up when converting to integers, NumPy’s floor() and ceil() functions are perfect:

import pandas as pd
import numpy as np

# Create a sample DataFrame with float values
data = {'Product': ['Laptop', 'Phone', 'Tablet', 'Monitor', 'Keyboard'],
        'Price': [999.99, 699.50, 349.75, 249.25, 89.99]}

df = pd.DataFrame(data)

# Always round down (floor)
df['Price_Floor'] = np.floor(df['Price']).astype(int)

# Always round up (ceiling)
df['Price_Ceil'] = np.ceil(df['Price']).astype(int)

print(df)

Output:

    Product   Price  Price_Floor  Price_Ceil
0   Laptop   999.99         999        1000
1   Phone    699.50         699         700
2   Tablet   349.75         349         350
3   Monitor  249.25         249         250
4   Keyboard  89.99          89          90

This method is useful for scenarios like inventory management (where you might always round up) or budget calculations (where you might always round down).

Read pd.crosstab Function in Python

Method 5 – Handle NaN Values During Conversion

When working with real-world data, you’ll often encounter missing values. Converting a DataFrame with NaN values directly to integers will raise an error. Here’s how to handle this situation:

import pandas as pd
import numpy as np

# Create a DataFrame with NaN values
data = {'Product': ['Laptop', 'Phone', 'Tablet', 'Monitor', 'Keyboard'],
        'Price': [999.99, np.nan, 349.75, 249.25, np.nan]}

df = pd.DataFrame(data)
print("Original DataFrame with NaN values:")
print(df)

# Method 1: Fill NaN values before conversion
df['Price_Int_1'] = df['Price'].fillna(0).astype(int)

# Method 2: Use pandas.to_numeric with errors='coerce' and then fill
df['Price_Int_2'] = pd.to_numeric(df['Price'], errors='coerce').fillna(0).astype(int)

print("\nDataFrame after handling NaN values:")
print(df)

Output:

Original DataFrame with NaN values:
    Product   Price
0   Laptop   999.99
1   Phone      NaN
2   Tablet   349.75
3   Monitor  249.25
4   Keyboard   NaN

DataFrame after handling NaN values:
    Product   Price  Price_Int_1  Price_Int_2
0   Laptop   999.99         999         999
1   Phone      NaN           0           0
2   Tablet   349.75         349         349
3   Monitor  249.25         249         249
4   Keyboard   NaN           0           0

This approach is crucial when working with datasets that have missing values, which is common in real-world scenarios like customer surveys or sales data with gaps.

When working with Pandas DataFrames, converting float values to integers is a common requirement. The method you choose depends on your specific needs:

  • Use astype(int) for simple truncation
  • Combine round() with astype(int) when you need proper rounding
  • Apply custom functions with apply() for complex logic
  • Use NumPy’s floor() or ceil() for consistent rounding direction
  • Handle NaN values with fillna() before conversion

In this article, I have explained some methods to convert a float to an integer in Pandas, which are: using astype(), round() with astype(), apply(), numpy.floor or numpy.ceil(), and handling NaN values during conversion.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.