Float columns in Pandas often show long decimals or scientific notation. Formatting them by rounding, adding separators, or scaling improves readability while keeping the underlying data unchanged.
This article covers simple ways to format floats in Pandas.
1. Round Off Float Values
You can round float values to a fixed number of decimal places using pd.options.display.float_format.
import pandas as pd
data = {'Month': ['January', 'February', 'March', 'April'],
'Expense': [21525220.653, 31125840.875, 23135428.768, 56245263.942]}
df = pd.DataFrame(data, columns=['Month', 'Expense'])
pd.options.display.float_format = '{:.2f}'.format
print(df)
Output
Month Expense 0 January 21525220.65 1 February 31125840.88 2 March 23135428.77 3 April 56245263.94
Explanation:
- pd.options.display.float_format: Sets the display format for floats in Pandas.
- '{:.2f}'.format: Rounds values to 2 decimal places in fixed-point notation.
2. Format with Separators and Decimal Precision
Adding commas as thousand separators and controlling decimal precision improves clarity.
import pandas as pd
data = {'Product': ['Laptop', 'Phone', 'Tablet', 'Desktop'],
'Price': [1200.50, 799.99, 349.75, 1500.25]}
df = pd.DataFrame(data, columns=['Product', 'Price'])
pd.options.display.float_format = '{:,.2f}'.format
df['Price'] = df['Price'].apply(lambda x: '{:,.2f}'.format(x))
print(df)
Output
Product Price 0 Laptop 1,200.50 1 Phone 799.99 2 Tablet 349.75 3 Desktop 1,500.25
Explanation:
- '{:,.2f}'.format: Formats numbers with commas and 2 decimal places.
- .apply(lambda x: ...): Applies the formatting to each element in the column.
3. Scale and Format Numbers
Large numbers can be hard to interpret. Scaling values and applying formatting makes the data easier to read.
import pandas as pd
data = {'City': ['New York', 'Los Angeles', 'Chicago', 'Houston'],
'Population': [833687, 390400, 271000, 232800]}
df = pd.DataFrame(data, columns=['City', 'Population'])
df['Population'] = df['Population'] / 100000
pd.options.display.float_format = '{:,.2f}'.format
print(df)
Output
City Population 0 New York 8.34 1 Los Angeles 3.90 2 Chicago 2.71 3 Houston 2.33
Explanation:
- Dividing by 1000000 scales numbers to millions.
- '{:,.2f}'.format rounds and adds separators.