py4u blog

Converting Floats to Integers in a Pandas DataFrame: A Comprehensive Guide

In data analysis with Python using the Pandas library, you often encounter situations where you need to convert float values to integer values within a DataFrame. This could be for various reasons such as simplifying data representation, improving memory efficiency (since integers generally take up less memory than floats in some contexts), or conforming to specific data requirements for downstream operations like database storage or certain types of statistical analyses. In this blog post, we'll explore different methods to achieve this conversion, along with best practices and example usage.

2026-06

Table of Contents#

  1. Using the astype() Method
  2. Using apply() with a Custom Function
  3. Handling NaN Values (Best Practice)
  4. Memory Considerations
  5. Example Usage Scenarios
  6. References

Using the astype() Method#

The astype() method in Pandas is a straightforward way to convert the data type of a column (or multiple columns) in a DataFrame. When dealing with floats that can be accurately represented as integers (i.e., they have no fractional part), this method works well.

Syntax#

import pandas as pd
 
# Assume df is your DataFrame and 'column_name' is the column with float values
df['column_name'] = df['column_name'].astype('int64')

Here, 'int64' is the integer data type specification. You can also use 'int32' depending on your memory and range requirements (though 'int64' is more common as it can handle a wider range of values).

Example#

import pandas as pd
 
data = {'values': [1.0, 2.0, 3.0]}
df = pd.DataFrame(data)
df['values'] = df['values'].astype('int64')
print(df)

Output:

   values
0       1
1       2
2       3

Using apply() with a Custom Function#

If you want more control over the conversion process (for example, if you need to perform additional operations before converting), you can use the apply() method along with a custom function.

Syntax#

import pandas as pd
 
def float_to_int(x):
    return int(x)  # Simple conversion, you can add more logic here
 
df['column_name'] = df['column_name'].apply(float_to_int)

Example#

import pandas as pd
 
data = {'values': [1.5, 2.7, 3.3]}
df = pd.DataFrame(data)
 
def float_to_int(x):
    return int(x)  # Just truncating the decimal part here
df['values'] = df['values'].apply(float_to_int)
print(df)

Output:

   values
0       1
1       2
2       3

Note: This method simply truncates the decimal part. If you want to round the values instead, you can modify the function to use round(x) instead of int(x).

Handling NaN Values (Best Practice)#

When converting float columns that might contain NaN (Not a Number) values, you need to handle them appropriately. The astype() method will raise an error if there are NaN values. So, a best practice is to first check for and handle NaN values.

Using fillna() before astype()#

import pandas as pd
 
data = {'values': [1.0, float('nan'), 3.0]}
df = pd.DataFrame(data)
df['values'] = df['values'].fillna(0).astype('int64')  # Filling NaN with 0 here
print(df)

Output:

   values
0       1
1       0
2       3

Using apply() with pd.isna() check#

import pandas as pd
 
data = {'values': [1.0, float('nan'), 3.0]}
df = pd.DataFrame(data)
 
def float_to_int_with_nan(x):
    if pd.isna(x):
        return 0  # Custom handling for NaN
    return int(x)
 
df['values'] = df['values'].apply(float_to_int_with_nan)
print(df)

Output:

   values
0       1
1       0
2       3

Memory Considerations#

Integers generally take up less memory than floats in Pandas (especially when using appropriate integer data types like int32 for smaller ranges). You can check the memory usage of a DataFrame before and after conversion using the memory_usage() method.

Example#

import pandas as pd
 
data = {'values': [1.0, 2.0, 3.0]}
df = pd.DataFrame(data)
print("Before conversion memory usage:", df.memory_usage())
 
df['values'] = df['values'].astype('int64')
print("After conversion memory usage:", df.memory_usage())

Output (example values, actual may vary based on system):

Before conversion memory usage: Index        80
values      32
dtype: int64
After conversion memory usage: Index        80
values      24
dtype: int64

You can see that the memory usage for the values column has decreased.

Example Usage Scenarios#

Financial Data#

Suppose you have a DataFrame with stock prices that were recorded as floats (e.g., 100.0 for $100 per share). Converting them to integers can simplify calculations related to share counts and total values.

Sensor Data#

If you have sensor data where the measured values (originally floats) are actually whole numbers (e.g., counts of events per second), converting to integers can make the data more intuitive and reduce memory overhead.

References#

By following the methods and best practices outlined in this blog post, you can effectively convert float columns to integer columns in your Pandas DataFrames depending on your specific data requirements and scenarios.