Categories
Python Answers

How to display Python Pandas DataFrame of floats using a format string for columns?

Spread the love

To display Python Pandas DataFrame of floats using a format string for columns, we use the map method.

For instance, we write

import pandas as pd
df = pd.DataFrame([123.4567, 234.5678, 345.6789, 456.7890],
                  index=['foo','bar','baz','quux'],
                  columns=['cost'])
df['foo'] = df['cost']
df['cost'] = df['cost'].map('${:,.2f}'.format)

to create a data frame df with DataFrame.

Then we call call map on the df['cost'] column to return a string with the value interpolated to the format string with format.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *