TypeError: unsupported format string passed to Series.__format__(TypeError:不支持的格式字符串传递给 Series.__format__)
问题描述
我正在尝试在我的仪表板中添加一千个分隔符,但我所有的努力都是徒劳的.发现此解决方案更准确,但它给了我一个错误
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"]]
.
这篇关于TypeError:不支持的格式字符串传递给 Series.__format__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:TypeError:不支持的格式字符串传递给 Series.__format__
基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01