首页 > 解决方案 > 如何自定义格式化 pandas 整数列以用逗号作为千位分隔符显示?

问题描述

我有一个数据框列GDP/year,来自一个关于多年来自杀的数据集。此列的数据类型当前是对象(字符串),但我希望它为整数。

这些值是逗号分隔的,所以我不能直接将它们转换为整数。我尝试字符串删除逗号,存储为整数,然后我再次引入逗号,但它的类型恢复为对象。

数据集:https ://www.kaggle.com/russellyates88/suicide-rates-overview-1985-to-2016

# convert to int...
suicides[' gdp_for_year ($) '] = suicides[' gdp_for_year ($) '].str.replace(',','').astype(int) 
# now reformat with commas as thousands separator...
suicides[' gdp_for_year ($) '] = suicides[' gdp_for_year ($) '].astype(int).apply(lambda x: "{:,}".format(x)) 
# ...wanted to get dtype integer, but it's back to object

标签: pythonpandasseparatorcustom-formatting

解决方案


您正在将每个元素转换为字符串:"{:,}".format(x)

但我想你想在你的 pandas DataFrame 中显示你的数字以默认显示逗号分隔符,为此你可以这样做,但对于浮点数据类型:

pd.options.display.float_format = '{:,}'.format

如果您还想要 int 类型,则应该使用 monkey-patch pandas.io.formats.format.IntArrayFormatter


推荐阅读