Search code examples
pythonpandasdata-sciencedata-analysis

How to convert float into int in pandas?


This is my code:

users.age.mean().astype(int64)

(where users is the name of dataframe and age is a column in it)

This is the error I am getting:

AttributeError                            
Traceback (most recent call last)
<ipython-input-29-10b672e7f7ae> in <module>
----> 1 users.age.mean().astype(int64)
AttributeError: 'float' object has no attribute 'astype'

Solution

  • users.age.mean() returns a float not a series. Floats don't have astype, only pandas series.

    Try:

    x = numpy.int64(users.age.mean())

    Or:

    x = int(users.age.mean())