dtype: integer, but loc returns float dtype: integer, but loc returns float pandas pandas

dtype: integer, but loc returns float


You get back a float because each row contains a mix of float and int types. Upon selecting a row index with loc, integers are cast to floats:

>>> df.loc[4]year          1979.000000firms       390352.000000age              1.000000survival         0.774522Name: 4, dtype: float64

So choosing the age entry here with df.loc[4, 'age'] would yield 1.0.

To get around this and return an integer, you could use loc to select from just the age column and not the whole DataFrame:

>>> df['age'].loc[4]1


This was a bug in pandas up through version 0.19. It seems to have been fixed in version 0.20. cf. https://github.com/pandas-dev/pandas/issues/11617


Do you have to use loc? What about this:

otherDf.loc(df['age'][idx])

Grabbing the value through the 'age' Series returns the appropriate type (int64)