Skip to content Skip to sidebar Skip to footer

Py Pandas .format(dataframe)

As Python newbie I recently discovered that with Py 2.7 I can do something like: print '{:20,.2f}'.format(123456789) which will give the resulting output: 123,456,789.00 I'm now

Solution 1:

import pandas as pd
import numpy as np
data = np.random.random((8,3))*10000
df = pd.DataFrame (data)
pd.options.display.float_format = '{:20,.2f}'.format
print(df)

yields (random output similar to)

                     0                    1                    2
0             4,839.01             6,170.02               301.63
1             4,411.23             8,374.36             7,336.41
2             4,193.40             2,741.63             7,834.42
3             3,888.27             3,441.57             9,288.64
4               220.13             6,646.20             3,274.39
5             3,885.71             9,942.91             2,265.95
6             3,448.75             3,900.28             6,053.93

The docstring for pd.set_option or pd.describe_option explains:

display.float_format: [default: None] [currently: None] : callable
        The callable should accept a floating point number andreturn
        a string with the desired format of the number. This is used
        in some places like SeriesFormatter.
        See core.format.EngFormatter for an example.

Post a Comment for "Py Pandas .format(dataframe)"