Delete rows in dataframe using list in Pandas

This is a general question about filtering a pandas frame using a list. The problem is this:

  • I have a pandas dataframe dfwith a columnfield
  • I have a list of forbidden fields, for example ban_field=['field1','field2','field3']
  • All items are ban_fielddisplayed indf.field

At the moment, to extract a data frame without a forbidden field, I do the following:

for f in ban_field:
    df = df[df.field!=f]

Is there a more pythonic way to continue (on one line?)?

+4
source share
2 answers

Method # 1: use the isinlogical array selector too:

In [47]: df = pd.DataFrame({"a": [2]*10, "field": range(10)})

In [48]: ban_field = [3,4,6,7,8]

In [49]: df[~df.field.isin(ban_field)]
Out[49]: 
   a  field
0  2      0
1  2      1
2  2      2
5  2      5
9  2      9

[5 rows x 2 columns]

Method number 2: use query:

In [51]: df.query("field not in @ban_field")
Out[51]: 
   a  field
0  2      0
1  2      1
2  2      2
5  2      5
9  2      9

[5 rows x 2 columns]
+8
source

isin (~).

df[~df.field.isin(ban_field)]
+1

All Articles