Reputation: 63
Say I have population data stored in a column of a dataframe using pandas in python with Country names as row indices. How do I convert the whole column of numbers into string thousands separator using commas. Basically, I need 12345678,integer, converted into 12,345,678.
Upvotes: 6
Views: 4460
Reputation: 67968
You can use regex as well.
df['Population'].str.replace(r"(?!^)(?=(?:\d{3})+$)", ",")
See demo.
https://regex101.com/r/cVYAGw/1
Upvotes: 1
Reputation: 76917
Using apply
to format the numbers.
In [40]: ps.apply('{:,}'.format)
Out[40]:
CountryA 12,345,678
CountryB 3,242,342
dtype: object
In [41]: ps
Out[41]:
CountryA 12345678
CountryB 3242342
dtype: int64
Upvotes: 10