Pandas select only numeric or integer field from dataframe

后端 未结 4 1823
栀梦
栀梦 2021-01-11 19:17

I have this Pandas dataframe (df):

     A    B
0    1    green
1    2    red
2    s    blue
3    3    yellow
4    b    black

A type is obje

4条回答
  •  温柔的废话
    2021-01-11 19:41

    Note that convert_objects is deprecated

    >>> df[['A']].convert_objects(convert_numeric=True)
    __main__:1: FutureWarning: convert_objects is deprecated.  Use the data-type specific converters pd.to_datetime, pd.to_timedelta and pd.to_numeric.
    

    From 0.17.0: use pd.to_numeric, set errors='coerce' so that incorrect parsing returns NaN. Use notnull to return a boolean mask to use on the original dataframe:

    >>> df[pd.to_numeric(df.A, errors='coerce').notnull()]
       A       B
    0  1   green
    1  2     red
    3  3  yellow
    

提交回复
热议问题