pandas how to check dtype for all columns in a dataframe?

后端 未结 3 638
一生所求
一生所求 2021-01-30 00:05

It seems that dtype only work for pandas.DataFrame.Series, right? Is there a function to display data types of all columns at once?

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-30 00:43

    The singular form dtype is used to check the data type for a single column. And the plural form dtypes is for data frame which returns data types for all columns. Essentially:

    For a single column:

    dataframe.column.dtype
    

    For all columns:

    dataframe.dtypes
    

    Example:

    import pandas as pd
    df = pd.DataFrame({'A': [1,2,3], 'B': [True, False, False], 'C': ['a', 'b', 'c']})
    
    df.A.dtype
    # dtype('int64')
    df.B.dtype
    # dtype('bool')
    df.C.dtype
    # dtype('O')
    
    df.dtypes
    #A     int64
    #B      bool
    #C    object
    #dtype: object
    

提交回复
热议问题