[英]Converting a full column of integer into string with thousands separated using comma in pandas
Say I have population data stored in a column of a dataframe using pandas in python with Country names as row indices. 假设我使用python中的pandas将国家名称作为行索引存储在数据框列中的人口数据。 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.
基本上,我需要12345678,整数,转换成12,345,678。
Using apply
to format the numbers. 使用
apply
来格式化数字。
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
You can use regex as well. 你也可以使用正则表达式。
df['Population'].str.replace(r"(?!^)(?=(?:\d{3})+$)", ",")
See demo. 见演示。
https://regex101.com/r/cVYAGw/1 https://regex101.com/r/cVYAGw/1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.