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

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

df['Dollar Amount']=df['Dollar Amount'].apply('{:,}'.format)

using map:

df['Dollar Amount'] = df['Dollar Amount'].map("{:,}".format)

you can also use style which is nicer and let you do all your styling in one line:

df = df.style.format({'Dollar Amount': "{:,}"})

Here's a solution using locale that might help, as long as you're okay with formatting your numbers as strings:

import pandas as pd
import locale as lc

# Get the list of all locale options
all_locales = lc.locale_alias
# I'll use US conventions since that's what you mentioned in your question
lc.setlocale(lc.LC_ALL,all_locales["en_us"])

df = pd.DataFrame({"Dollar Amount":[1000, 2000000, 2500.01]})
df["Dollars Formatted"] = df["Dollar Amount"].apply(lambda x: "$"+lc.format("%.2f",x,True))

The convenient thing about locale is that you can easily change between different number conventions if you need to, and it will continue to apply those conventions for the millions and billions separators.


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