Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to add thousand separator to numbers in Python pandas DataFrame

Assuming that I have a pandas dataframe and I want to add thousand separators to all the numbers (integer and float), what is an easy and quick way to do it?

like image 949
thatMeow Avatar asked Jan 03 '17 15:01

thatMeow


People also ask

How do you add thousand separators 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 add a prefix to all columns in Pandas?

Pandas Series: add_prefix() function The add_prefix() function is used to prefix labels with string prefix. For Series, the row labels are prefixed. For DataFrame, the column labels are prefixed. The string to add before each label.

How do I add all the values to a DataFrame in Python?

To sum all the rows of a DataFrame, use the sum() function and set the axis value as 1. The value axis 1 will add the row values.


1 Answers

Assuming you just want to display (or render to html) the floats/integers with a thousands separator you can use styling which was added in version 0.17.1:

import pandas as pd
df = pd.DataFrame({'int': [1200, 320], 'flt': [5300.57, 12000000.23]})

df.style.format('{:,}')

To render this output to html you use the render method on the Styler.

like image 77
lcvriend Avatar answered Sep 29 '22 14:09

lcvriend