Pandas: grep like function

fred picture fred · Sep 27, 2012 · Viewed 8.5k times · Source

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

Answer

Wouter Overmeire picture Wouter Overmeire · Sep 27, 2012

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