Convert Floats to Integers in a pandas DataFrame
In this tutorial, you will convert DataFrame columns of type float to integer.
TLDR solution
df['column_a'] = df['column_a'].astype('int')
# if column_a has NaN values
df['column_a'] = df['column_a'].fillna(0).astype('int')
Example 1: Convert Floats to Integers in a Specific Column
Let's say, you have the following DataFrame:
data = { "fish": ["salmon", "pufferfish", "shark", "mackerel"],
"count": [2.0, 35, 53.0, 88]}
df = pd.DataFrame(data)
print(df)
print(df.dtypes)
fish count
0 salmon 2.0
1 pufferfish 35.0
2 shark 53.0
3 mackerel 88.0
fish object
count float64
dtype: object
That is, column count is of type float.
You can use the astype function to convert the floats to integers:
df['count'] = df['count'].astype('int')
print(df)
print(df.dtypes)
fish count
0 salmon 2
1 pufferfish 35
2 shark 53
3 mackerel 88
fish object
count int64
dtype: object
Example 2: When all Columns are of Type Float
Suppose:
data = { "size": [50.0, 22.0, 39.0, 20.0],
"count": [2.0, 35.0, 53.0, 88.0]}
df = pd.DataFrame(data)
print(df)
print(df.dty
size count
0 50.0 2.0
1 22.0 35.0
2 39.0 53.0
3 20.0 88.0
size float64
count float64
dtype: object
Convert the entire DataFrame like this:
df = df.astype('int')
print(df)
print(df.dtypes)
size count
0 50 2
1 22 35
2 39 53
3 20 88
size int64
count int64
Example 3: Convert Floats in a Mixed Type DataFrame
Suppose:
data = { "fish": ["salmon", "pufferfish", "shark", "mackerel"],
"size": [50.0, 22.0, 39.0, 20.0],
"count": [2.0, 35.0, 53.0, 88.0]}
df = pd.DataFrame(data)
print(df)
print(df.dtypes)
fish size count
0 salmon 50.0 2.0
1 pufferfish 22.0 35.0
2 shark 39.0 53.0
3 mackerel 20.0 88.0
fish object
size float64
count float64
dtype: object
Convert the two columns of type float like this:
df[['size', 'count']] = df[['size', 'count']].astype('int')
print(df)
print(df.dtypes)
fish size count
0 salmon 50 2
1 pufferfish 22 35
2 shark 39 53
3 mackerel 20 88
fish object
size int64
count int64
dtype: object
Example 4: Convert Floats to Integers When NaN Values are present
Suppose:
data = { "fish": ["salmon", "pufferfish", "shark", "mackerel"],
"count": [2.0, 35, np.nan, 88]}
df = pd.DataFrame(data)
print(df)
print(df.dtypes)
fish count
0 salmon 2.0
1 pufferfish 35.0
2 shark NaN
3 mackerel 88.0
fish object
count float64
dtype: object
Replace NaN values with zeros first using fillna(0), then convert:
df['count'] = df['count'].fillna(0).astype('int')
print(df)
print(df.dtypes)
fish count
0 salmon 2
1 pufferfish 35
2 shark 0
3 mackerel 88
fish object
count int64
dtype: object
That's it! You just converted floats to integers in a DataFrame.