'float'对象没有属性'astype'

Sit*_*ogz 1 python median pandas

我试图在数据帧的列中找到中值.我得到的中值是浮点数,但我需要整数格式.

c_med = round(df['count'].median().astype(int))

c_med = round(df['count'].median()).astype(int)
Run Code Online (Sandbox Code Playgroud)

以上两种类型都给我这个错误.如果astype(int)删除,那么答案是正确的.

错误

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'
Run Code Online (Sandbox Code Playgroud)

cri*_*007 8

在函数之后,您不再使用Pandas API round.

你必须像这样抛出价值

c_med = int(round(df['count'].median()) 
Run Code Online (Sandbox Code Playgroud)

我对熊猫不太熟悉,但你可以试试

c_med = df['count'].median().round().astype(int)
Run Code Online (Sandbox Code Playgroud)