Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas – Return rows that only contain integers

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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")

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading