Is there a grep like built-in function in Pandas to drop a row if it has some string or value? Thanks in advance.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With