pd.set_option('display.float_format', ...)
is used to set the formatting options for floating-point numbers when they are displayed in the console or output. It allows you to customize how floating-point numbers are presented, including the number of decimal places, scientific notation, and other formatting details.
import pandas as pd
# Sample DataFrame
data = {'A': [1234567.890123456789, 9876543.210987654321]}
df = pd.DataFrame(data)
# Set display float format to show 4 decimal places
pd.set_option('display.float_format','{:,.2f}'.format)
# Display the DataFrame
print(df)
A
0 1,234,567.89
1 9,876,543.21
In this example, the pd.set_option('display.float_format', '{:,.2f}'.format)
line sets the floating-point number format to display up to two decimal places with comma as a thousand separator. The formatting options are specified using a format string, where '{:,.2f}'
indicates that each floating-point number should be displayed with up to two decimal places.
You can customize the format string to meet your specific formatting requirements. Here are some examples:
'{:.2f}'
: Display up to two decimal places.'{:,.2f}'
: Display up to two decimal places with a comma as a thousands separator.
Note that this setting affects the display of floating-point numbers in Pandas DataFrames when you print or view them in a Jupyter Notebook or console. It doesn’t change the actual values in the DataFrame; it only affects their presentation.
Using a Lambda function:
pd.set_option('display.float_format', lambda x: '%.2f' % x)