To modify the float output do this:

df= pd.DataFrame(range(5), columns=['a'])
df.a = df.a.astype(float)
df

Out[33]:

          a
0 0.0000000
1 1.0000000
2 2.0000000
3 3.0000000
4 4.0000000

pd.options.display.float_format = '{:,.0f}'.format
df

Out[35]:

   a
0  0
1  1
2  2
3  3
4  4
Answer from EdChum on Stack Overflow
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas convert float to integer in dataframe
Pandas Convert Float to Integer in DataFrame - Spark By {Examples}
November 22, 2024 - 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 ...
Discussions

python - Converting Float to Int on certain columns in a data frame - Stack Overflow
I am trying to convert columns 0 to 4 and 6 to ints from there current float types. I tried: df[0:4,6].astype(int) but of course this does not work... More on stackoverflow.com
🌐 stackoverflow.com
Pandas. Convert float to integer only if it is a round number.
I can't promise this is the best answer, but could be worth considering anyway. I would convert the data to strings: >>> stringify = lambda i: f'{i}' if i % 1 else f'{i:.0f}' >>> df.cost = df.cost.apply(stringify, axis=1) Then write to a file. This will ensure the data is written to a file in the format you want it. Proof: >>> cost = [1.5, 1.0, 2.6, 4.0] >>> list(map(stringify, cost)) ['1.5', '1', '2.6', '4'] However, note that if you subsequently open it in a "smart" viewer like Excel that tries to auto-detect data types, there's no guarantee it will remain in that format. More on reddit.com
🌐 r/learnpython
7
1
April 12, 2021
Python: Convert column from float to int - Stack Overflow
I am using a pandas, and need to concat two dataframes based on a set_id index. One of the dataframes has these as floats (see below). How can I convert these to ints? More on stackoverflow.com
🌐 stackoverflow.com
May 15, 2017
How to convert sparse pandas dataframe with `NaN` into integer values?
On my phone, so have not tested the code. #apparently this is not working #df[df.notnull()] = df[df.notnull()].apply(lambda x: int(x)) #int_slice = df.notnull() #df[int_slice] = df[int_slice].astype('int64') Ok, that did not work. Then first to get series for each column (or row) and then applying some if else to get ints. #this horrible piece of oneliner is working df = df.apply(lambda col: col.apply(lambda x: int(x) if pd.notnull(x) else x), axis=1) Still not convinced that they are really ints. You should use ´float_format´ when saving to csv. Or when saving the df use formatting to remove decimals ´'{:.0f}' (or '%.8f')´ df.to_csv(path, float_format='%.0f') There are probably better ways to handle your problem. edit. added float_format and oneliner More on reddit.com
🌐 r/learnpython
14
2
August 26, 2016
🌐
GeeksforGeeks
geeksforgeeks.org › convert-floats-to-integers-in-a-pandas-dataframe
Convert Floats to Integers in a Pandas DataFrame - GeeksforGeeks
December 1, 2023 - In this example, the code begins by importing the NumPy module as 'np.' It then displays the data types of DataFrame 'df.' After that, it converts the 'Field_2' column from float to int using NumPy's int64 data type and displays the updated data types.
🌐
Saturn Cloud
saturncloud.io › blog › how-to-read-rows-and-convert-float-to-integer-in-pandas
How to Read Rows and Convert Float to Integer in Pandas | Saturn Cloud Blog
December 6, 2023 - Once we have read in our data, we can start converting float to integer using Pandas. The most common way to convert a float to an integer in Pandas is to use the astype() method, which allows us to cast a column to a different data type.
🌐
w3resource
w3resource.com › python-exercises › pandas › python-pandas-data-frame-exercise-51.php
Pandas: Convert the datatype of a given column(floats to ints) - w3resource
September 6, 2025 - import pandas as pd import numpy as np exam_data = {'name': ['Anastasia', 'Dima', 'Katherine', 'James', 'Emily', 'Michael', 'Matthew', 'Laura', 'Kevin', 'Jonas'], 'score': [12.5, 9.1, 16.5, 12.77, 9.21, 20.22, 14.5, 11.34, 8.8, 19.13], 'attempts': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1], 'qualify': ['yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes']} df = pd.DataFrame(exam_data) print("Original DataFrame:") print(df) print("\nData types of the columns of the said DataFrame:") print(df.dtypes) print("\nNow change the Data type of 'score' column from float to int:") df.score = df.score.astype(int) print(df) print("\nData types of the columns of the DataFrame now:") print(df.dtypes)
🌐
Statology
statology.org › home › how to convert floats to integers in pandas
How to Convert Floats to Integers in Pandas
June 1, 2022 - This tutorial explains how to convert floats to integers in a pandas DataFrame, including an example.
🌐
Python Guides
pythonguides.com › convert-floats-to-integer-in-pandas
How To Convert Float To Int In Pandas
May 26, 2025 - The simplest way to convert float values to integers in Pandas is to use the astype() method in Python.
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas convert column to int in dataframe
Pandas Convert Column to Int in DataFrame - Spark By {Examples}
June 26, 2025 - You can also use DataFrame.apply() method to convert Fee column from string to integer in pandas. As you see in this example we are using numpy.int64. Before going to use numpy functions we need to import numpy module. 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)
🌐
Data to Fish
datatofish.com › floats-to-integers-dataframe
How to Convert Floats to Integers in a pandas DataFrame
df['column_a'] = df['column_a'... 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:...
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.to_numeric.html
pandas.to_numeric — pandas 3.0.1 documentation - PyData |
Convert dtypes. ... >>> s = pd.Series(["1.0", "2", -3]) >>> pd.to_numeric(s) 0 1.0 1 2.0 2 -3.0 dtype: float64 >>> pd.to_numeric(s, downcast="float") 0 1.0 1 2.0 2 -3.0 dtype: float32 >>> pd.to_numeric(s, downcast="signed") 0 1 1 2 2 -3 dtype: int8 >>> s = pd.Series(["apple", "1.0", "2", -3]) >>> pd.to_numeric(s, errors="coerce") 0 NaN 1 1.0 2 2.0 3 -3.0 dtype: float64
🌐
Reddit
reddit.com › r/learnpython › pandas. convert float to integer only if it is a round number.
r/learnpython on Reddit: Pandas. Convert float to integer only if it is a round number.
April 12, 2021 -

I have been trying to automate a task which i've been doing in Excel but have come up against a little stumbling block that I'm not sure of the best solution to resolve.

I have a dataframe, with a number of columns where the values are all floats. When processing the sheet in Excel to xml, all of the float values get exported as whole numbers, but the Pandas export currently has them as floats with a trailing decimal.

So for example I may have a df column:

sample = {"cost":[1.5, 1.0, 2.6, 4.0}

and I want this to output as

cost
1.5
1
2.6
4

i.e not having 1.0 and 4.0.

I tried,

df["cost"] = df["cost"].apply(lambda x: int(x) if x % 1 == 0 else x)

but this still just output float values. As a test, I tried changing the else conditional to a string

df["cost"] = df["cost"].apply(lambda x: int(x) if x % 1 == 0 else "test")

This correctly updated the round numbers to the desired integer, and converted the floats to "test". so I am not sure why my original solution doesn't work. I also tried else float(x) but this left every result as a float.

Is a lambda solution the best way to go about updating this, or would Pandas have something more suitable built in?

What am I doing wrong in the way I have tried this?

Any help greatly appreciated!

🌐
YouTube
youtube.com › watch
How to turn NaN and floats into integers by using Pandas dataframe (df) - YouTube
#pandas #nan #float #dataanalysis In this tutorial, learn how to convert NaN values and float numbers to integers using pandas DataFrame in Python. We'll cov...
Published   August 2, 2024
🌐
Saturn Cloud
saturncloud.io › blog › how-to-convert-a-float64-column-to-int64-in-pandas
How to Convert a Float64 Column to Int64 in Pandas | Saturn Cloud Blog
January 25, 2024 - Pandas, a widely used Python library for data analysis and manipulation, offers a range of functions to facilitate the cleaning and transformation of data. A typical operation involves converting a float64 column to an int64 column, and in the following article, we'll delve into the steps to ...
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.convert_dtypes.html
pandas.DataFrame.convert_dtypes — pandas documentation
By using the options convert_string, convert_integer, convert_boolean and convert_floating, it is possible to turn off individual conversions to StringDtype, the integer extension types, BooleanDtype or floating extension types, respectively. For object-dtyped columns, if infer_objects is True, ...
🌐
Codedamn
codedamn.com › news › python
Converting Float to Int in Python: A Step-by-Step Guide
July 3, 2023 - For instance, if you're writing a program that calculates the number of people in a room, a float number like 3.5 would not make sense. Python provides a built-in function int() that can convert a float to an integer.