Python 'float' 对象没有属性 'astype'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38626789/
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
'float' object has no attribute 'astype'
提问by Sitz Blogz
I am trying to find median values in a column of the dataframe. The median value that I am getting is float but I need it in integer format.
我试图在数据框的一列中找到中值。我得到的中值是浮点数,但我需要整数格式。
c_med = round(df['count'].median().astype(int))
c_med = round(df['count'].median()).astype(int)
Both the above types give me this error. If astype(int)
is removed then the answer is correct.
以上两种类型都给了我这个错误。如果astype(int)
被删除,那么答案是正确的。
Error
错误
Traceback (most recent call last):
File "all_median.py", line 16, in <module>
c_med = round(df['count'].median()).astype(int)
AttributeError: 'float' object has no attribute 'astype'
回答by OneCricketeer
You no longer are in the Pandas API after the round
function.
函数之后,您不再处于 Pandas API 中round
。
You have to cast the value like so
你必须像这样投射价值
c_med = int(round(df['count'].median())
I'm not too familiar with Pandas, but you could try
我对 Pandas 不太熟悉,但你可以试试
c_med = df['count'].median().round().astype(int)