get list of pandas dataframe columns based on data type

前端 未结 12 1405
后悔当初
后悔当初 2020-12-07 07:37

If I have a dataframe with the following columns:

1. NAME                                     object
2. On_Time                                      object
         


        
12条回答
  •  醉酒成梦
    2020-12-07 08:03

    If you want a list of only the object columns you could do:

    non_numerics = [x for x in df.columns \
                    if not (df[x].dtype == np.float64 \
                            or df[x].dtype == np.int64)]
    

    and then if you want to get another list of only the numerics:

    numerics = [x for x in df.columns if x not in non_numerics]
    

提交回复
热议问题