Consider this example:
import pandas as pd
from decimal import Decimal
my_df = pd.DataFrame(columns=["a", "b", "c"])
my_df = my_df.append({'a': 1, 'b': 3.0/7, 'c': Decimal(3.0/7)}, ignore_index=True)
print(my_df)
print("-------")
with pd.option_context('float_format', '{:.4f}'.format, 'display.expand_frame_repr', False):
print(my_df)
This prints out:
a b c
0 1 0.428571 0.42857142857142854763807804374664556235074996...
-------
a b c
0 1 0.4286 0.42857142857142854763807804374664556235074996...
Being aware that one can control the printout of number of decimals in a float in Pandas DataFrame with pd.option_context('float_format',..., I have tried to apply the same approach to an element, which is of decimal.Decimal class. As the test code printout shows:
- the float printout has been truncated from
0.428571to0.4286, as expected - however, the Decimal object is still printed with a ton of decimals
I'd like to keep the Decimal objects in my Pandas DataFrame, however at certain points, I'd like to print them with a smaller, limited number of decimals - like what with pd.option_context('float_format',... does for floats.
Is this controlled "truncated/rounded printout" of Decimal in a Pandas DataFrame possible - and if so, how can it be done?