python - Remove lines in dataframe using a list in Pandas -
it's generic question filtering pandas dataframe using list. problem following:
- i have pandas dataframe
df
columnfield
- i have list of banned fields, example
ban_field=['field1','field2','field3']
- all elements of
ban_field
appear indf.field
for moment, retrieve dataframe without banned field, proceed follows:
for f in ban_field: df = df[df.field!=f]
is there more pythonic way proceed (in 1 line?)?
method #1: use isin
, boolean array selector:
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]: field 0 2 0 1 2 1 2 2 2 5 2 5 9 2 9 [5 rows x 2 columns]
method #2: use query
:
in [51]: df.query("field not in @ban_field") out[51]: field 0 2 0 1 2 1 2 2 2 5 2 5 9 2 9 [5 rows x 2 columns]
Comments
Post a Comment