How to Add Thousand Separator to Numbers in Python Pandas Dataframe

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

When formatting a number with , you can just use '{:,}'.format:

n = 10000
print '{:,}'.format(n)
n = 1000.1
print '{:,}'.format(n)

In pandas, you can use the formatters parameter to to_html as discussed here.

num_format = lambda x: '{:,}'.format(x)
def build_formatters(df, format):
return {
column:format
for column, dtype in df.dtypes.items()
if dtype in [ np.dtype('int64'), np.dtype('float64') ]
}
formatters = build_formatters(data_frame, num_format)
data_frame.to_html(formatters=formatters)

Adding the thousands separator has actually been discussed quite a bit on stackoverflow. You can read here or here.

How to insert a comma as a thousands separator in a pandas dataframe column?

Notice it will convert your float type to object

df.DollarAmount.apply(lambda x : "{:,}".format(x))
Out[509]:
0 5,721.48
1 4,000.0
2 4,769.0
3 824.07
4 643.6
5 620.0
Name: DollarAmount, dtype: object

Formatting thousand separator for numbers in a pandas dataframe

Comma is the default separator. If you want to choose your own separator you can do this by declaring the sep parameter of pandas to_csv() method.

df.to_csv(sep=',')

If you goal is to create thousand separators and export them back into a csv you can follow this example:

import pandas as pd
df = pd.DataFrame([[12172083.89, 1341.4078, -9568703.592, 10323.7222],
[21661725.86, -1770.2725, 12669066.38, 14669.7118]],columns=['A','B','C','D'])
for c in df.columns:
df[c] = df[c].apply(lambda x : '{0:,}'.format(x))
df.to_csv(sep='\t')

If you just want pandas to show separators when printed out:

pd.options.display.float_format = '{:,}'.format
print(df)

XlsxWriter with Pandas dataframe thousand separator

It should work. You need to move the add_format() a bit later in your code, after you get a reference to the workbook object. Here is an example:

import pandas as pd


# Create a Pandas dataframe from some data.
df = pd.DataFrame({'Data': [1234.56, 234.56, 5678.92]})

# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('pandas.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel(writer, sheet_name='Sheet1')

# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets['Sheet1']

# Set a currency number format for a column.
num_format = workbook.add_format({'num_format': '#,###'})
worksheet.set_column('B:B', None, num_format)

# Close the Pandas Excel writer and output the Excel file.
writer.save()

Output:

enter image description here

Formatting thousand separator for integers in a pandas dataframe

The formatters parameter in to_html will take a dictionary of column names mapped to a formatting function. Below has an example of a function to build a dict that maps the same function to both floats and ints.

In [250]: num_format = lambda x: '{:,}'.format(x)

In [246]: def build_formatters(df, format):
...: return {column:format
...: for (column, dtype) in df.dtypes.iteritems()
...: if dtype in [np.dtype('int64'), np.dtype('float64')]}
...:

In [247]: formatters = build_formatters(df_int, num_format)


In [249]: print df_int.to_html(formatters=formatters)
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>20,000</td>
</tr>
<tr>
<th>1</th>
<td>10,000</td>
</tr>
</tbody>
</table>

Rounding and thousand separator in pandas dataframe: cannot get rid of decimal

the round() function returns a floating point number, which means you'll still get that decimal in the string representation.

>>> round(3.1)
3.0

An easy way to get rid of the decimal is to cast it to an integer:

>>> int(round(3.1))
3

In your code, this would be:

df = pd.DataFrame({'name':['abc', 'bvn'],'val' : [100000.1234,2000000.1234]})
df['val'] = df['val'].apply(lambda x: '{:,}'.format(int(np.round(x,0))))
print(df)


Related Topics



Leave a reply



Submit