Use pandas DataFrame.astype(int)
and DataFrame.apply() methods to cast float column to integer(int/int64) type. I believe you would know float is bigger than int type, so you can easily downcase but the catch is you would lose any value after the decimal. Note that while casting it doesn’t do any rounding and flooring and it just truncates the fraction values (anything after .). In this article, I will explain different ways to convert columns with float values to integer values.
Key Points –
- Use
astype()
to convert float columns to integers by specifying'int'
or'int64'
as the target type. - Converting from float to integer may result in data loss due to truncation of decimal places.
- You can convert multiple columns at once by selecting them and applying
astype()
. - Using
.astype()
ensures the DataFrame retains its structure but may cause issues with overflows in large numbers. - Converting float to integer may impact performance, especially with large datasets, due to the change in memory usage.
- NaN values in float columns must be handled before conversion, as integers do not support NaN.
Quick Examples of Pandas Convert Float to Integer
If you are in a hurry, below are some of the quick examples of how to convert float to integer type in DataFrame.
# Quick examples of pandas convert float to integer
# Converting "Fee" from float to int
# Using DataFrame.astype()
df["Fee"]=df["Fee"].astype(int)
print(df.dtypes)
# Converting "Fee" and "Discount" from float to int
# Using DataFrame.astype()
df = df.astype({"Fee":"int","Discount":"int"})
print(df.dtypes)
# Convert "Fee" from float to int
# Using DataFrame.apply(np.int64)
df["Fee"] = df["Fee"].apply(np.int64)
print(df.dtypes)
# Converting "Fee" and "Discount" from float to int
# Using DataFrame.apply(np.int64)
df["Fee"] = df["Fee"].apply(np.int64)
df["Discount"] = df["Discount"].apply(np.int64)
# Convert "Fee" from float to int and replace NaN values
df['Fee'] = df['Fee'].fillna(0).astype(int)
print(df)
print(df.dtypes)
Now, let’s create a DataFrame with a few rows and columns and execute some examples and validate the results. Our DataFrame contains column names Courses
, Fee
, Duration
and Discount
.
# Create DataFrame
import pandas as pd
import numpy as np
technologies= {
'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
'Fee' :[22000.30,25000.40,23000.20,24000.50,26000.10],
'Duration':['30days','50days','35days', '40days','55days'],
'Discount':[1000.10,2300.15,1000.5,1200.22,2500.20]
}
df = pd.DataFrame(technologies)
print(df)
print(df.dttypes)
Yields below output.
# Output:
Courses Fee Duration Discount
0 Spark 22000.3 30days 1000.10
1 PySpark 25000.4 50days 2300.15
2 Hadoop 23000.2 35days 1000.50
3 Python 24000.5 40days 1200.22
4 Pandas 26000.1 55days 2500.20
Courses object
Fee float64
Duration object
Discount float64
dtype: object
Pandas Convert Float to int (Integer)
Use pandas DataFrame.astype()
function to convert float to int (integer), you can apply this on a specific column. Below example converts Fee
column to int32
from float64
. You can also use numpy.dtype
as a param to this method.
# Convert "Fee" from float to int
df["Fee"]=df["Fee"].astype(int)
print(df)
print(df.dtypes)
Yields below output.
# Output:
Courses Fee Duration Discount
0 Spark 22000 30days 1000.10
1 PySpark 25000 50days 2300.15
2 Hadoop 23000 35days 1000.50
3 Python 24000 40days 1200.22
4 Pandas 26000 55days 2500.20
Courses object
Fee int32
Duration object
Discount float64
dtype: object
Casting Multiple Columns From Float to Integer
Similarly, you can also convert multiple columns from float to integer by sending dict of column name -> data type to astype()
method. The below example converts both columns Fee
and Discoun
t to int types.
# Converting "Fee" and "Discount" from float to int
df = df.astype({"Fee":"int","Discount":"int"})
print(df.dtypes)
Yields below output.
# Output:
Courses object
Fee int32
Duration object
Discount int32
dtype: object
Using apply(np.int64) to Cast From Float to Integer
You can also use DataFrame.apply() method to convert Fee
column from float to integer in pandas. As you see in this example we are using numpy.dtype (np.int64).
import numpy as np
# Convert "Fee" from float to int
# Using DataFrame.apply(np.int64)
df["Fee"] = df["Fee"].apply(np.int64)
print(df.dtypes)
Yields below output.
# Output:
Courses object
Fee int64
Duration object
Discount float64
dtype: object
Convert Column Containing NaNs to astype(int)
In order to demonstrate some NaN/Null
values, let’s create a DataFrame using NaN Values. To convert a column that includes a mixture of float and NaN values to int, first replace NaN values with zero on pandas DataFrame and then use astype()
to convert.
import pandas as pd
import numpy as np
technologies= {
'Fee' :[22000.30,25000.40,np.nan,24000.50,26000.10,np.nan]
}
df = pd.DataFrame(technologies)
print(df)
print(df.dtypes)
Use .fillna()
to replace the NaN values with integer value zero. For Example df['Fee']=df['Fee'].fillna(0).astype(int)
method.
# Convert "Fee" from float to int and replace NaN values
df['Fee'] = df['Fee'].fillna(0).astype(int)
print(df)
print(df.dtypes)
Yields below output.
# Output:
Fee
0 22000
1 25000
2 0
3 24000
4 26000
5 0
Fee int32
dtype: object
FAQ on Pandas Convert Float to Integer in DataFrame
To convert a single column from float to integer in a Pandas DataFrame, you can use the astype()
method.
You can convert multiple columns from float to integer at once using the astype()
method.
To round the float values before converting them to integers in a Pandas DataFrame, you can use the round()
method before applying the astype(int)
conversion. This ensures the values are rounded to the nearest integer rather than truncated.
You can convert all float columns in a DataFrame to integers at once. To do this, you can use select_dtypes()
to target only the float columns, and then apply the conversion using astype(int)
.
If you want to convert a float column to an integer without modifying the original DataFrame, you can use the astype()
method without reassigning it back to the DataFrame. This way, the conversion is temporary, and the original DataFrame remains unchanged.
Conclusion
In this article, you have learned how to convert float column to integer in DataFrame using DataFrame.astype(int)
and DataFrame.apply()
method. Also, you have learned how to convert float to integers when you have Nan/null values in a column.
Happy Learning !!
Related Articles
- Pandas Convert String to Integer
- How to Convert Pandas DataFrame to List?
- Pandas Convert Integer to Datetime Type
- Pandas Convert Datetime to Date Column
- Pandas Convert Column to Int in DataFrame
- Pandas Convert List of Dictionaries to DataFrame
- Pandas – Convert DataFrame to Dictionary (Dict)
- Pandas Convert Multiple Columns To DateTime Type
- How to Convert String to Float in Pandas DataFrame
- How to Replace Nan/Null to Empty String in Pandas
- How to Convert Index to Column in pandas DataFrame.