应用格式错误

2024-05-29 11:57:15 发布

您现在位置:Python中文网/ 问答频道 /正文

soldpricemean = '{:,}'.format(bedbath_df.sold_price.mean().astype(int))

出于某种原因,这种格式化浮点运算的方法是在我编写的一个程序中进行的,而不是我正在处理的当前程序。我得到的错误是:

AttributeError: 'float' object has no attribute 'astype'

有人能解释一下为什么会这样吗?谢谢!在


Tags: 方法程序formatdfobject错误floatmean
1条回答
网友
1楼 · 发布于 2024-05-29 11:57:15

问题不在于格式。正如错误明确指出的那样,float对象没有属性astype。我假设您是从另一种语言得到的,但是在Python中,这不是您更改类型的方式。在

如果要将float转换为int,语法是int(float),而不是{}。你需要代码soldpricemean = '{:,}'.format(int(bedbath_df.sold_price.mean()))

相关问题 更多 >

    热门问题