Arthur Yang
Arthur Yang

Reputation: 17

pandas float output format? (pd.to_csv)

I have a DataFrame with some float numbers, i.e.:

Date    Value
Sep-1   1.2
Sep-2   2.34
Sep-3   10.453

I hope if I could write the DataFrame to a text file with following format:

Date    Value
Sep-1   0001.200
Sep-2   0002.340
Sep-3   0010.453

I tried float_foramt='%4.3f' but it doesn't work.

Upvotes: 0

Views: 530

Answers (2)

AC at CA
AC at CA

Reputation: 735

The function can simply be:

str('{:0>8.3f}'.format(x))

The format method is very powerful for this.

Upvotes: 0

BENY
BENY

Reputation: 323266

Is this what you need ?

df.Value.apply(lambda x : "{0:.3f}".format(x)).str.pad(8,side='left', fillchar='0')
Out[333]: 
0    0001.200
1    0002.340
2    0010.453
Name: Value, dtype: object

Upvotes: 2

Related Questions