Find all columns of dataframe in Pandas whose type is float, or a particular type?

前端 未结 3 1124
暖寄归人
暖寄归人 2020-12-13 02:08

I have a dataframe, df, that has some columns of type float64, while the others are of object. Due to the mixed nature, I cannot use

df.fillna(\'unknown\')          


        
相关标签:
3条回答
  • 2020-12-13 02:18

    You can see what the dtype is for all the columns using the dtypes attribute:

    In [11]: df = pd.DataFrame([[1, 'a', 2.]])
    
    In [12]: df
    Out[12]: 
       0  1  2
    0  1  a  2
    
    In [13]: df.dtypes
    Out[13]: 
    0      int64
    1     object
    2    float64
    dtype: object
    
    In [14]: df.dtypes == object
    Out[14]: 
    0    False
    1     True
    2    False
    dtype: bool
    

    To access the object columns:

    In [15]: df.loc[:, df.dtypes == object]
    Out[15]: 
       1
    0  a
    

    I think it's most explicit to use (I'm not sure that inplace would work here):

    In [16]: df.loc[:, df.dtypes == object] = df.loc[:, df.dtypes == object].fillna('')
    

    Saying that, I recommend you use NaN for missing data.

    0 讨论(0)
  • 2020-12-13 02:25

    This is conciser:

    # select the float columns
    df_num = df.select_dtypes(include=[np.float])
    # select non-numeric columns
    df_num = df.select_dtypes(exclude=[np.number])
    
    0 讨论(0)
  • 2020-12-13 02:35

    As @RNA said, you can use pandas.DataFrame.select_dtypes. The code using your example from a question would look like this:

    for col in df.select_dtypes(include=['object']).columns:
        df[col] = df[col].fillna('unknown')
    
    0 讨论(0)
提交回复
热议问题