Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to format the Dollar Amount column to have a comma thousands separator for easier viewing, but I haven't been able to figure it out. Can someone please show me the way?

import pandas as pd
df = pd.read_excel('filename.xlsx') 
df['Dollar Amount'].head()

Index  Dollar Amount
0      5721.48
1      4000.00
2      4769.00
3       824.07
4       643.60
5       620.00

Name: Dollar Amount, dtype: float64
like image 363
ACH Avatar asked Nov 21 '17 02:11

ACH


2 Answers

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
like image 133
BENY Avatar answered Oct 09 '22 03:10

BENY


This is a more pandorable way to get the thousands separator.

df['Dollar Amount']=df['Dollar Amount'].apply('{:,}'.format)
like image 33
Niranjan Avatar answered Oct 09 '22 03:10

Niranjan