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

后端 未结 3 637
一生所求
一生所求 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:32

    To go one step further, I assume you want to do something with these dtypes. df.dtypes.to_dict() comes in handy.

    my_type = 'float64' #<---
    
    dtypes = dataframe.dtypes.to_dict()
    
    for col_nam, typ in dtypes.items():
        if (typ != my_type): #<---
            raise ValueError(f"Yikes - `dataframe['{col_name}'].dtype == {typ}` not {my_type}")
    

    You'll find that Pandas did a really good job comparing NumPy classes and user-provided strings. For example: even things like 'double' == dataframe['col_name'].dtype will succeed when .dtype==np.float64.

    0 讨论(0)
  • 2021-01-30 00:38

    Suppose df is a pandas DataFrame then to get number of non-null values and data types of all column at once use:

    df.info()
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题