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
Pandas. Convert float to integer only if it is a round number.
python - pandas rounding when converting float to integer - Stack Overflow
What are possible causes of pandas converting an INT to Float?
Pandas - convert float value to minutes and seconds in CSV
Videos
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!
You are right, astype(int) does a conversion toward zero:
โintegerโ or โsignedโ: smallest signed int dtype
from pandas.to_numeric documentation (which is linked from astype() for numeric conversions).
If you want to round, you need to do a float round, and then convert to int:
df.round(0).astype(int)
Use other rounding functions, according your needs.
the output is always a bit random as the 'real' value of an integer can be slightly above or below the wanted value
Floats are able to represent whole numbers, making a conversion after round(0) lossless and non-risky, check here for details.
If I understand right you could just perform the rounding operation followed by converting it to an integer?
s1 = pd.Series([1.2,2.9])
s1 = s1.round().astype(int)
Which gives the output:
0 1
1 3
dtype: int32
I don't use float at all in my program and randomly I'm getting an (easy to fix) bug that an input requires Int and float was provided. Here is some recent code, but this isnt the first time something like this happened. I'm looking for a general reasoning rather than this particular reasoning.
x=df.loc[((df['FROM2'] > 599) & (df['FROM2'] < 700) & (df['y']==True))]
z=pd.concat([z, x])then later in the code...
a= pd.merge(a, z, how = 'outer', indicator = True)
a= a.loc[a['_merge'] == 'left_only'].copy()
a.drop(columns = '_merge', inplace = True)