Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

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.

1 Answer

0 votes
by (41.4k points)

You can use this below line of code to check whether a variable is numeric or not:

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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...