3

Minimal working example:

import pandas as pd
df = pd.DataFrame({
    'letters':('A', 'B', 'C'),
    'numbers':(0.1111, 0.2222, 0.3333)
}).set_index('letters')
df.style.format('{:.2f}')

Result:

Out[4]: <pandas.io.formats.style.Styler at 0x7f963f826eb8>

What I want:

Something similar to the default Jupyter output:

jupyter_output

1 Answer 1

4

One way to do this is using the formatters or float_format keyword of df.to_string

>>> print(df.to_string(float_format=lambda x: '{:.2f}'.format(x)))
         numbers
letters         
A           0.11
B           0.22
C           0.33

float_format will apply a function to all floats to format, while formatters can accept a list or dict to format specific columns in the dataframe.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.