在 pandas 中使用逗号将整数的整列转换为成千上万的字符串 [英] Converting a full column of integer into string with thousands separated using comma in pandas

查看:96
本文介绍了在 pandas 中使用逗号将整数的整列转换为成千上万的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我将人口数据存储在使用Country name作为行索引的python中的pandas的数据帧的列中.如何使用逗号将整列数字转换为字符串千位分隔符. 基本上,我需要12345678,整数,转换为12,345,678.

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.

推荐答案

使用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

这篇关于在 pandas 中使用逗号将整数的整列转换为成千上万的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆