在熊猫中使用逗号将整数的整列转换为成千上万的字符串

Uda*_*nta 5 python regex dataframe pandas

假设我使用国家名称作为行索引的python中的熊猫将人口数据存储在数据框的列中。如何使用逗号将整列数字转换为字符串千位分隔符。基本上,我需要12345678,integer,转换为12,345,678。

Zer*_*ero 5

使用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
Run Code Online (Sandbox Code Playgroud)

  • 您可以通过locale.setlocale(locale.LC_NUMERIC,“ en_IN”)来做到这一点。请参阅http://stackoverflow.com/a/14238258 (2认同)