python - 在 pandas 中转换为字符串之前进行舍入

标签 python pandas numpy dataframe

我在舍入方面遇到问题,这似乎很常见,但我无法通过谷歌搜索找到答案,所以我决定在这里提问。

这是我的数据

        day         reg     log ad      trans   paid
1111    20171005    172     65  39.0    14.0    3.0
1112    20171006    211     90  46.0    17.0    4.0
1113    20171007    155     70  50.0    17.0    1.0
1114    20171008    174     71  42.0    18.0    0.0
1115    20171009    209     63  43.0    21.0    2.0

这就是我所做的,我仍然想要 % 的数字

table['% log'] = (table.log / table.reg * 100).astype(str) + '%'
table['% ad'] = (table.ad / table.reg * 100).astype(str) + '%'
table['% trans'] = (table.trans / table.reg* 100).astype(str) + '%'
table['% paid'] = (table.paid / table.reg * 100).astype(str) + '%'

这就是我得到的,需要最后的四舍五入

        day         reg     log ad      trans   paid    % log            % ad       % trans     % paid
1111    20171005    172     65  39.0    14.0    3.0     37.7906976744%  22.6744186047%  8.13953488372%  1.74418604651%
1112    20171006    211     90  46.0    17.0    4.0     42.654028436%   21.8009478673%  8.05687203791%  1.89573459716%
1113    20171007    155     70  50.0    17.0    1.0     45.1612903226%  32.2580645161%  10.9677419355%  0.645161290323%
1114    20171008    174     71  42.0    18.0    0.0     40.8045977011%  24.1379310345%  10.3448275862%  0.0%
1115    20171009    209     63  43.0    21.0    2.0     30.1435406699%  20.5741626794%  10.04784689%    0.956937799043%

我想要的是百分比不要太长,四舍五入到两位数即可。

最佳答案

您需要round :

table['% log'] = (table.log / table.reg * 100).round(2).astype(str) + '%'

更好的解决方案是按子集选择所有列并将join输出到原始df:

cols = ['log','ad','trans','paid']
table =(table.join(table[cols].div(table.reg, 0)
                              .mul(100)
                              .round(2)
                              .astype(str)
                              .add('%')
                              .add_prefix('%% ')))
print (table)
           day  reg  log    ad  trans  paid   % log    % ad % trans % paid
1111  20171005  172   65  39.0   14.0   3.0  37.79%  22.67%   8.14%  1.74%
1112  20171006  211   90  46.0   17.0   4.0  42.65%   21.8%   8.06%   1.9%
1113  20171007  155   70  50.0   17.0   1.0  45.16%  32.26%  10.97%  0.65%
1114  20171008  174   71  42.0   18.0   0.0   40.8%  24.14%  10.34%   0.0%
1115  20171009  209   63  43.0   21.0   2.0  30.14%  20.57%  10.05%  0.96%

此外,如果需要更好的输出 - 添加 0 表示 2 位小数:

table =(table.join(table[cols].div(table.reg, 0)
                              .mul(100)
                              .applymap("{0:.2f}".format)
                              .add('%')
                              .add_prefix('%% ')))
print (table)
           day  reg  log    ad  trans  paid   % log    % ad % trans % paid
1111  20171005  172   65  39.0   14.0   3.0  37.79%  22.67%   8.14%  1.74%
1112  20171006  211   90  46.0   17.0   4.0  42.65%  21.80%   8.06%  1.90%
1113  20171007  155   70  50.0   17.0   1.0  45.16%  32.26%  10.97%  0.65%
1114  20171008  174   71  42.0   18.0   0.0  40.80%  24.14%  10.34%  0.00%
1115  20171009  209   63  43.0   21.0   2.0  30.14%  20.57%  10.05%  0.96%

关于python - 在 pandas 中转换为字符串之前进行舍入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46705080/

相关文章:

python - 使用 Python 实现神经网络的成本函数(第 5 周 Coursera)

python - python 中只有左基的峰日珥的累积和

python - 匹配 pandas 中的两组相同的字符以及中间的一些字符

python - 从一个数组中删除另一个数组中的元素

python - 使用 for 循环比较列表的位置

python - 从表单输入,GAE,错误

python - 如何向下复制 pandas DataFrame 值以填充 0?

python - 如何将多行中的值放在一行中(用逗号分隔)

python - Scipy 循环方差

python - 将 SKLearn 癌症数据集加载到 Pandas DataFrame 中