pandas 使用python将数字(基于计算)格式化为两位小数的百分比
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/22369020/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
format number (based on calculation) as a percentage to two decimal places using python
提问by yoshiserry
The values: budget = 11,000 actual = 10,000 variance = budget - actual (1,000)
值:预算 = 11,000 实际 = 10,000 差异 = 预算 - 实际 (1,000)
total, would be the value of budget variable: 11,000
My Code:
我的代码:
percent_val = variance/total
format_percent = {:.2f}.format(percent_val)
return format_percent
I Thought the above code would retun the value 9.09 (at two decimal places)
我认为上面的代码会返回值 9.09(小数点后两位)
return value: 9.09
This video shows it, but I can't see to get it to work using the {0:2.df} string?
该视频显示了它,但我看不到使用 {0:2.df} 字符串使其正常工作?
http://www.youtube.com/watch?v=mmJPx6YsOMI
HOW DO I format the 9.09 percent as a number and not a string so I can do calculations with it later?
我如何将 9.09% 格式化为数字而不是字符串,以便我以后可以用它进行计算?
回答by user2357112 supports Monica
You forgot to make a string:
你忘了做一个字符串:
format_percent = '{:.2f}'.format(percent_val)
#                ^      ^
Also, if you want a percent, you'll need to multiply by 100, and if you're on Python 2 (I can't tell), you'll either need to use floats or from __future__ import division.
另外,如果你想要一个百分比,你需要乘以 100,如果你使用的是 Python 2(我不知道),你需要使用浮点数或from __future__ import division.
If you want to roundthe number to two decimal places, rather than creating formatted output, there's the roundfunction:
如果要将数字四舍五入到小数点后两位,而不是创建格式化输出,则可以使用以下round函数:
rounded = round(percent_val, 2)
Then your output will be a float instead of a string, and you can keep doing math with it.
然后你的输出将是一个浮点数而不是一个字符串,你可以继续用它做数学运算。
回答by Andy Hayden
You can plug the display format into pandas' display options:
您可以将显示格式插入到 pandas 的显示选项中:
In [11]: df = pd.DataFrame(np.random.randn(2, 2))
In [12]: df
Out[12]:
          0         1
0  1.058814 -0.011675
1 -0.002627 -0.152505
In [13]: pd.options.display.float_format = '{:.2f}'.format
In [14]: df
Out[14]:
      0     1
0  1.06 -0.01
1 -0.00 -0.15
See more about python's string formatting here.
在此处查看有关 python 字符串格式的更多信息。
Note: the numbers themselves are unaffected (they haven't been rounded):
注意:数字本身不受影响(它们没有被四舍五入):
In [15]: df.iloc[0, 0]
Out[15]: 1.058814403984879
回答by J. Khoury
In the event you decide to use pandas and df, here is a quick methodif you don't mind having all your pd data set to a specific precision, and as you can see the data can still be used with its original precision.
如果您决定使用 pandas 和 df,如果您不介意将所有 pd 数据设置为特定精度,这里有一个快速方法,并且如您所见,数据仍然可以以其原始精度使用。
import pandas as pd
import numpy as np
pd.set_option('precision',2)
df = pd.DataFrame(np.random.randn(5,2), columns = ['A','B'])
df
Out[15]: 
      A     B
0 -1.87  1.20
1 -0.55 -1.19
2  1.04  0.89
3 -0.65  0.30
4  0.07 -1.37
df.A[0] + 1.77777
Out[16]: -0.095449113301297794

