How to determine whether a column/variable is numeric or not in Pandas/NumPy?

后端 未结 9 1998
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 02:47

Is there a better way to determine whether a variable in Pandas and/or NumPy is numeric or not ?

I have a self defined

9条回答
  •  悲&欢浪女
    2020-12-01 03:15

    In pandas 0.20.2 you can do:

    import pandas as pd
    from pandas.api.types import is_string_dtype
    from pandas.api.types import is_numeric_dtype
    
    df = pd.DataFrame({'A': ['a', 'b', 'c'], 'B': [1.0, 2.0, 3.0]})
    
    is_string_dtype(df['A'])
    >>>> True
    
    is_numeric_dtype(df['B'])
    >>>> True
    

提交回复
热议问题