Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all rows that meet regex condition

trying to teach myself pandas.. and playing around with different dtypes

I have a df as follows

df = pd.DataFrame({'ID':[0,2,"bike","cake"], 'Course':['Test','Math','Store','History'] })
print(df)
    ID  Course
0   0   Test
1   2   Math
2   bike    Store
3   cake    History

the dtype of ID is of course an object. What I want to do is remove any rows in the DF if the ID has a string in it.

I thought this would be as simple as..

df.ID.filter(regex='[\w]*')

but this returns everything, is there a sure fire method for dealing with such things?

like image 489
Umar.H Avatar asked Sep 07 '18 21:09

Umar.H


People also ask

How do I delete rows based on DataFrame conditions?

Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).

How do I delete rows in pandas based on multiple conditions?

Pandas provide data analysts a way to delete and filter data frame using dataframe. drop() method. We can use this method to drop such rows that do not satisfy the given conditions.

How do you delete a row that contains a certain value in R?

First of all, create a data frame. Then, use single square subsetting with apply function to remove rows that contains a specific number.

How do I remove all rows in a DataFrame?

DataFrame. drop() method you can drop/remove/delete rows from DataFrame. axis param is used to specify what axis you would like to remove. By default axis = 0 meaning to remove rows.


1 Answers

You can using to_numeric

df[pd.to_numeric(df.ID,errors='coerce').notnull()]
Out[450]: 
  Course ID
0   Test  0
1   Math  2
like image 119
BENY Avatar answered Oct 05 '22 18:10

BENY