Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display 2 decimal places, and use commas to separate thousands, in Jupyter/pandas?

Tags:

I'm working with pandas 0.18 in Jupyter.

I'd like to configure Jupyter/pandas to display 2 decimal places throughout, and to use comma separators in thousands.

How can I do this?

like image 695
Richard Avatar asked Aug 26 '16 12:08

Richard


People also ask

How do I set a thousand separator in pandas?

We use the python string format syntax '{:,. 0f}'. format to add the thousand comma separators to the numbers. Then we use python's map() function to iterate and apply the formatting to all the rows in the 'Median Sales Price' column.

How do you specify 2 decimal places in Python?

In Python, to print 2 decimal places we will use str. format() with “{:. 2f}” as string and float as a number. Call print and it will print the float with 2 decimal places.

How do I limit decimal places in Python DataFrame?

Lets use the dataframe. round() function to round off all the decimal values in the dataframe to 3 decimal places. Output : Example #2: Use round() function to round off all the columns in dataframe to different places.


2 Answers

Configure the following option in any cell:

pandas.options.display.float_format = '{:,.2f}'.format 

You can also format the output for any float throughout the notebook with this magic command:

%precision %.2f 
like image 94
IanS Avatar answered Oct 25 '22 18:10

IanS


You can set the display.precision option with e.g.:

pd.set_option('precision', 7) 

when you are done, reset it back using

pd.reset_option('precision') 

as documented in https://pandas.pydata.org/pandas-docs/stable/user_guide/options.html

like image 45
CAV Avatar answered Oct 25 '22 17:10

CAV