[英]How to store values in a Pandas DataFrame as a percentage and not a string
I'm using pandas to create data frames which will then be imported into PowerBI for visualization. 我正在使用熊猫创建数据框,然后将其导入PowerBI以进行可视化。 One of the columns in the data frame is a percentage calculation.
数据框中的一列是百分比计算。
I have no issues calculating the values. 我没有问题计算值。 However, these values appear without the '%' sign at the end, eg 55.2 as opposed to 55.2%.
但是,这些值的末尾没有'%'符号,例如55.2,而不是55.2%。
An example of my initial dataframe: 我的初始数据框的一个示例:
df1 =
year_per pass fail total
---------------------------------
201901 300 700 1000
201902 400 600 1000
201903 200 800 1000
201904 500 500 1000
I then calculate two new columns to state the % of the total that each column represent, such that the new data frame is: 然后,我计算两个新列,以说明每个列所代表的总数的百分比,以使新数据框为:
df2 =
year_per pass fail total pass% fail%
---------------------------------------------------
201901 300 700 1000 30.0 70.0
201902 400 600 1000 40.0 60.0
201903 200 800 1000 20.0 80.0
201904 500 500 1000 50.0 50.0
These new % columns are created using the following code: 这些新的%列是使用以下代码创建的:
df2['pass%'] = round((df1['pass'] / df1['total']) * 100,1)
Which works. 哪个有效。 PowerBI is happy to use those values.
PowerBI很高兴使用这些值。 However, I'd like it to display the '%' sign at the end for clarity.
但是,为了清楚起见,我希望它在最后显示“%”符号。 Therefore, I updated the calculation code to:
因此,我将计算代码更新为:
df2['pass%'] = (round((df1['pass'] / df1['total']) * 100,1).astype(str))+'%'
This also produces the right output, visually. 这也会在视觉上产生正确的输出。 However, as the values are now strings, PowerBI can't process the new values as the visualization is expecting a number format, not a string.
但是,由于这些值现在是字符串,因此PowerBI无法处理新值,因为可视化文件期望数字格式而不是字符串。
I've also tried using the following formatting (as mentioned here: how to show Percentage in python ): 我也尝试过使用以下格式(如此处所述: 如何在python中显示百分比 ):
{0:.1f}%".format()
ie: 即:
df2['pass%'] = '{0:.1f}%'.format(round((df1['pass'] / df1['total']) * 100,1))
but get the error: 但得到错误:
'TypeError: unsupported format string passed to Series.__format__'
Therefore, I was wondering if there is a way to store the values as a number format with the % sign following the numbers? 因此,我想知道是否有一种方法可以将值存储为数字格式,并在数字后加上%符号? Otherwise I'll just have to live with the values without the % sign.
否则,我将只使用不带%号的值。
This is, because you pass a series to round
, which it expects a scalar numeric argument, but gets a series (also format
would have a problem with a series). 这是因为,您将一个系列传递给
round
,它期望一个标量数值参数,但得到一个系列( format
也将对系列产生问题)。 You can do instead: 您可以改为:
df2['pass%'] = (df1['pass'] / df1['total']).map(lambda num: '{0:.1f}%'.format(round(num * 100, 1))
But you know, in contrast to the title of your question, this would of course store the percentage as a string. 但是您知道,与您的问题的标题相反,这当然会将百分比存储为字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.