Home > Blockchain >  Pandas - Return rows that only contain integers
Pandas - Return rows that only contain integers

Time:02-03

From a whole DataFrame, i picked a specific column.

Once I have this column (which is of type object) i want to return all the rows that have integers only.

Consider the following DataFrame.

Fruits     Vegetable 
    
Apple      Corn
123        Potatoe
subDf=df["Fruits"]

What can i use on subDf so that it returns all rows that contain only integers?

(In this case "123")

CodePudding user response:

You may use str.contains here:

subDf = df[df["Fruits"].str.contains(r'^\d $')]

str.contains uses the following regular expression, which means:

^    from the start of the Fruits value
\d   match one or more digits (integer only)
$    end of the value
  •  Tags:  
  • Related