1

What is the dtype at the last line here in the output, i am confused -

df.dtypes
#A     int64
#B      bool
#C    object
#dtype: object

Here i am only asking about the last line which is 'dtype: object'. I am not asking why it is not str, my question is what is this line for?

Maleficent
  • 57
  • 8

1 Answers1

1

df.dtypes returns a Series with each column name as index and the dtype as value. When pandas displays Series is also displays the overall type of the Series below it, which is what you see here (as type are objects).

This is the value of df.dtypes.dtype.

Example:

Why there is an extra line in the display

pd.Series([1, 2, 3])

0    1           # this is the Series data
1    2           #
2    3           #
dtype: int64        # this is just for display to indicate the Series type

Why the dtype is object

df = pd.DataFrame({'A': [1], 'B': [True], 'C': ['a']})

df.dtypes

A     int64
B      bool
C    object
dtype: object  # this just indicates that we have a Series of python objects
mozway
  • 194,879
  • 13
  • 39
  • 75
  • why the last line is mostly object is because it is containing multiple data types? – Maleficent Mar 23 '23 at 10:49
  • numpy/pandas are made to work with specific types in a vectorial way. Mostly numbers (or derivatives, like booleans, datetime, etc.) plus a few specific types like `string` (you can get a list [here](https://stackoverflow.com/questions/29245848/what-are-all-the-dtypes-that-pandas-recognizes), probably not up to date). In this case they are stored in a compact low-level format. Whenever you don't have those specific types, the dtype is `object`. – mozway Mar 23 '23 at 11:03