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 OverflowTo 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
Use the pandas.DataFrame.astype(<type>) function to manipulate column dtypes.
>>> df = pd.DataFrame(np.random.rand(3,4), columns=list("ABCD"))
>>> df
A B C D
0 0.542447 0.949988 0.669239 0.879887
1 0.068542 0.757775 0.891903 0.384542
2 0.021274 0.587504 0.180426 0.574300
>>> df[list("ABCD")] = df[list("ABCD")].astype(int)
>>> df
A B C D
0 0 0 0 0
1 0 0 0 0
2 0 0 0 0
EDIT:
To handle missing values:
>>> df
A B C D
0 0.475103 0.355453 0.66 0.869336
1 0.260395 0.200287 NaN 0.617024
2 0.517692 0.735613 0.18 0.657106
>>> df[list("ABCD")] = df[list("ABCD")].fillna(0.0).astype(int)
>>> df
A B C D
0 0 0 0 0
1 0 0 0 0
2 0 0 0 0
python - Converting Float to Int on certain columns in a data frame - Stack Overflow
Pandas. Convert float to integer only if it is a round number.
Python: Convert column from float to int - Stack Overflow
How to convert sparse pandas dataframe with `NaN` into integer values?
Videos
consider df
df = pd.DataFrame(np.random.rand(10, 10) * 10)

use np.r_ to get slc
slc = np.r_[0:4, 6]
df[slc] = df[slc].astype(int)
df
or pass a dictionary of types with keys as column names
df.astype({c: int for c in slc})

I was getting an error as some of my column values were NaN which obviously can not be converted to int. So a better approach would be to handle NaN before converting the datatype and avoid ValueError: Cannot convert non-finite values (NA or inf) to integer.
df['col_name'] = df['col_name'].fillna(0).astype(int)
This fills NaN with 0 and then converts to the desired datatype which is int in this case.
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!