Home > Enterprise >  Error while using str.contains for checking numeric values in a column using regex
Error while using str.contains for checking numeric values in a column using regex

Time:11-22

I have a dataframe. I want to check if a particular column has numeric values or not using regex matching. When I use str.contains it shows an error like below. What is the correct way to check if all the values in a column have numeric values or not?

df= 
 Roll.No  Words 
  20       two
  30       three
  40       four
  50       five
  60       Nan
  70       Nan
df = df[df['Roll.No'].str.contains(r'[^0-9]', na=True)]
Error: AttributeError: Can only use .str accessor with string values!

CodePudding user response:

You can use

df = df[df['Roll.No'].astype(str).str.contains(r'[^0-9]', na=True)]

With .astype(str), you will be able to run a regex on the numeric column.

The .str.contains(r'[^0-9]', na=True) expression will find all values that contain at least one char that is not a digit (like dots or commas).

  • Related