If we need to convert 104.0 to only 104, without using %g that converts it into string. Is there any method in python - pandas by which we can check if a column is float and I do not want to convert it to string or cast to integer, yet make only 140.0 to 140 and have 139.58 to 139.58. Please help?
Asked
Active
Viewed 186 times
1
-
1See [this answer](https://stackoverflow.com/a/47542713/4909087), you can skip the call to `round()`. – cs95 Dec 18 '19 at 23:15
-
Predominantly I am writing a data frame into a .csv and reading again from that csv and writing into a remote filer location. So I want the float data values not to have .0 at the end. I dont want to convert the floats into string. – Sushmitha Dec 18 '19 at 23:16
-
`.astype(object)` doesn't convert the column to a string. – cs95 Dec 18 '19 at 23:17
-
I do not want to convert it to object datatype, but I want to have it in same float format – Sushmitha Dec 18 '19 at 23:20
-
That is not possible. You can't have the cake and eat it too. Either you remain satisfied with the current representation or you convert the dtype to object (NOTE THAT THEY ARE STILL NUMBERS, but the operations are no longer vectorizable). – cs95 Dec 18 '19 at 23:21
-
Well my scenario is that I want it removed while converting it to csv. That is the format required as per business logic. I cant change them. Is there a better way? – Sushmitha Dec 18 '19 at 23:23
1 Answers
1
You can do it in (at least) two ways.
Let's say we have a Series that looks like this:
x = pd.Series([140.0, 120.0, 100.0])
x
0 140.0
1 120.0
2 100.0
dtype: float64
You can then use the astype() method:
x = x.astype(int)
x
0 140
1 120
2 100
dtype: int64
As for Python, remember you have the int() function.
x = int(140.0)
x
140

Enrique Ortiz Casillas
- 519
- 3
- 10
-
1no, but when I use int(143.89) the nit converts it into 144. But I want float to remain as float, but just want to remove .0 from other float numbers – Sushmitha Dec 18 '19 at 23:19