Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

what is dtype at the last line while checking dtypes for all columns in Python Pandas dataframes

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading