Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: grep like function

Tags:

grep

pandas

row

Is there a grep like built-in function in Pandas to drop a row if it has some string or value? Thanks in advance.

like image 696
fred Avatar asked Sep 27 '12 16:09

fred


People also ask

What is the equivalent of grep in python?

One of the most beneficial commands is the grep command. GREP is a useful command-line tool that lets us use regular expressions to search plain text files for specified lines. In Python, regular expressions (RE) are commonly used to determine whether a string matches a specific pattern.

How do you use STR in pandas?

str can be used to access the values of the series as strings and apply several methods to it. Pandas Series. str. contains() function is used to test if pattern or regex is contained within a string of a Series or Index.

What is regex pandas?

A regular expression (regex) is a sequence of characters that define a search pattern. To filter rows in Pandas by regex, we can use the str. match() method.


1 Answers

Have a look at df['column_label].str Below example will drop all rows where column A holds 'a' character and 'B' equals 20.

In [46]: df
Out[46]:
     A   B
0  foo  10
1  bar  20
2  baz  30

In [47]: cond = df['A'].str.contains('a') & (df['B'] == 20)

In [48]: df.drop(df[cond].index.values)
Out[48]:
     A   B
0  foo  10
2  baz  30
like image 174
Wouter Overmeire Avatar answered Oct 02 '22 16:10

Wouter Overmeire