TypeError:不支持的格式字符串传递给 Series.__format__

2022-01-21 00:00:00 python pandas plotly plotly-dash

问题描述

我正在尝试在我的仪表板中添加一千个分隔符,但我所有的努力都是徒劳的.发现此解决方案更准确,但它给了我一个错误

I am trying to add a thousand separator to my dash cards and all my efforts have been futile. This solution is found to be more accurate but it gives me an error

TypeError:传递给 Series 的格式字符串不受支持.格式

TypeError: unsupported format string passed to Series.format

@app.callback(
[Output('sls', 'children'),
Output('wngs', 'children'),
Output('rvne', 'children')],
Input('year', 'value')
)

def card_update(select_year):
dff=df_good.copy()
df_formattedd=dff.groupby(['year'], as_index=False)[['Net Sale', 'Winnings','Revenue']].sum()
df_formattedd[['Net Sale','Winnings','Revenue']]=df_formattedd[['Net Sale','Winnings','Revenue']].apply(lambda x:round(x,2))
df_formattedd[['Net Sale','Winnings','Revenue']]= df_formattedd[['Net Sale','Winnings','Revenue']].apply(lambda x: f'{x:,}')
sales=df_formattedd[df_formattedd['year']==select_year]['Net Sale']
winnings=df_formattedd[df_formattedd['year']==select_year]['Winnings']
revenue=df_formattedd[df_formattedd['year']==select_year]['Revenue']

return sales, winnings, revenue


解决方案

重现您的问题的简短抽象示例代码:

Short abstracted example code that reproduces your problem:

import pandas as pd

df = pd.DataFrame(
    {
        "A": [1000000.111, 1000000.222, 1000000.333],
        "B": [2000000.111, 2000000.222, 2000000.333],
        "C": [3000000.111, 3000000.222, 3000000.333],
    }
)

df[["A", "B", "C"]] = df[["A", "B", "C"]].apply(lambda x: round(x, 2))
df[["A", "B", "C"]] = df[["A", "B", "C"]].apply(lambda x: f'{x:,}')

给予:

TypeError:传递给 Series 的格式字符串不受支持.格式

TypeError: unsupported format string passed to Series.format

问题在于您尝试格式化数字的最后一行.

The problem is with the last line where you try to format the numbers.

问题在于 lambda 函数中的 x 指的是 Pandas 系列而不是数字.Series 不支持您正在使用的格式化字符串类型:

The problem is that x in the lambda function refers to a Pandas Series and not a number. A Series doesn't support the type of formatting string you're using:

,"选项表示使用逗号作为千位分隔符.

The ',' option signals the use of a comma for a thousands separator.

https://docs.python.org/3/library/string.html

相反,您可以这样做:

import pandas as pd

df = pd.DataFrame(
    {
        "A": [1000000.111, 1000000.222, 1000000.333],
        "B": [2000000.111, 2000000.222, 2000000.333],
        "C": [3000000.111, 3000000.222, 3000000.333],
    }
)

df[["A", "B", "C"]] = df[["A", "B", "C"]].apply(lambda x: round(x, 2))
df[["A", "B", "C"]] = df[["A", "B", "C"]].apply(
    lambda series: series.apply(lambda value: f"{value:,}")
)

所以这里嵌套的apply背后的思想是:对于DataFrame中的每一列df[[A"、B"、C"]] 获取每一行值并将字符串格式化程序应用于它.行值只是浮点数,因此字符串格式化程序能够处理它.

So the idea behind the nested apply here is: For each column in the DataFrame df[["A", "B", "C"]] take each row value and apply the string formatter to it. The row values are just floats so the string formatter is able to handle that.

结果

>>> print(df)
              A             B             C
0  1,000,000.11  2,000,000.11  3,000,000.11
1  1,000,000.22  2,000,000.22  3,000,000.22
2  1,000,000.33  2,000,000.33  3,000,000.33

或者,您可以使用 pd.options 设置格式.display.float_format:

Alternatively you can set the format using pd.options.display.float_format:

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

请记住,这不仅仅适用于 df[[A"、B"、C"]].

Keep in mind that this is applies to more than just df[["A", "B", "C"]].

相关文章