floss
floss

Reputation: 2773

Pandas how to keep the LAST trailing zeros when exporting DataFrame into CSV

In this question, my goal is to preserve the last trailing zeros when exporting the DataFrame to CSV

My dataset looks like this:

EST_TIME    Open    High
2017-01-01  1.0482  1.1200    
2017-01-02  1.0483  1.1230
2017-01-03  1.0485  1.0521
2017-01-04  1.0480  1.6483
2017-01-05  1.0480  1.7401
...., ...., ....
2017-12-31  1.0486  1.8480

I import and create a DataFrame and save to CSV by doing this:

df_file = '2017.csv'
df.to_csv(df_file, index=False)
files.download(df_file)

When I view the CSV, I see this:

EST_TIME    Open    High
2017-01-01  1.0482  1.12   
2017-01-02  1.0483  1.123
2017-01-03  1.0485  1.0521
2017-01-04  1.048   1.6483
2017-01-05  1.048   1.7401
...., ...., ....
2017-12-31  1.0486  1.848

All the zeros at the end are gone. I want to preserve the trailing zeros when I save the CSV and I want it at 4 decimal place.

Could you please let me know how can I achieve this?

Upvotes: 5

Views: 6168

Answers (2)

Gustavo Gradvohl
Gustavo Gradvohl

Reputation: 712

Try this: Float format both to display your data with 4 decimal places and to save it with 4 decimal.

when reading to pandas:

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

when saving to CSV.

df.to_csv('your_file.csv', float_format='%.4f',index=False)

enter image description here

Upvotes: 5

cisco
cisco

Reputation: 888

You could call apply and use format as such before writing to csv:

df_file = '2017.csv'
df['Open'] = df['Open'].apply('{:0<6}'.format)
df['High'] = df['High'].apply('{:0<6}'.format)
df.to_csv(df_file, index=False)

To apply to all columns, you can use applymap:

df = df.applymap('{:0<6}'.format)

Hope this helps.

Upvotes: 2

Related Questions