Home > Mobile >  Convert object to integer without losing the sense
Convert object to integer without losing the sense

Time:12-05

I have a df with different type of digits. I want to convert them to more clear digits. First I tried float then int. First one, for example turns out 2 but suppose to be 2607.

Hope someone can help. Regards

x = ["2.6069999999999998","899.0", "1.974", "1.2309999999999999","882.0","839.0","525.0"]

xx = pd.DataFrame(x)

xx

Output:

    0
0   2.6069999999999998
1   899.0
2   1.974
3   1.2309999999999999
4   882.0
5   839.0
6   525.0

Expected Output:

    0
0   2607
1   899
2   1974
3   1231
4   882
5   839
6   525

CodePudding user response:

This would work:

x = ["2.6069999999999998","899.0", "1.974", "1.2309999999999999","882.0","839.0","525.0"]
xx = []

for y in x:
    y = float(y)
    while y != round(y):
        y *= 10
        y = round(y , 3)
    xx.append(int(y))

print(xx)

output:

[2607, 899, 1974, 1231, 882, 839, 525]

very ugly tho

  • Related