Back
Is there a better way to determine whether a variable in Pandas and/or NumPy is numeric or not ?
I have a self defined dictionary with dtypes as keys and numeric / not as values.
You can use this below line of code to check whether a variable is numeric or not:
import pandas as pdfrom pandas.api.types import is_string_dtypefrom pandas.api.types import is_numeric_dtypedf = pd.DataFrame({'A': ['a', 'b', 'c'], 'B': [1.0, 2.0, 3.0]})is_string_dtype(df['A'])>>>> Trueis_numeric_dtype(df['B'])>>>> True
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'])
31k questions
32.8k answers
501 comments
693 users