Sitz Blogz Sitz Blogz - 2 months ago 12x
Python Question

'float' object has no attribute 'astype'

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
is removed then the answer is correct.


Traceback (most recent call last):
File "", line 16, in <module>
c_med = round(df['count'].median()).astype(int)
AttributeError: 'float' object has no attribute 'astype'


You no longer are in the Pandas API after the round function.

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

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