'Rounding columns in a dataframe with file.dtypes.iteritems()

I'm making a function on rounding decimals, I wrote the following already:

for colname, coltype in file.dtypes.iteritems():
    if coltype in lst:
        file[colname] = file[colname].round(desired_decimals)

However, it does not round the columns it should round.



Solution 1:[1]

Two files are created, the values are written in the first in their own format, in the second in a string format.

import pandas as pd

df = pd.DataFrame({'A':[5.291752, 8.920429, 10.0]})

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

df.to_csv('string_no.csv', index=False, header=['A'])

with open('string_yes.csv', 'w') as file:
    file.writelines(df.to_string(index=False, header=['A']))

Output ordinary

enter image description here

Output string format

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1