Home > Net >  Python - drop elements from DataFrame
Python - drop elements from DataFrame

Time:07-13

I have DataFrame where are few columns. I have also list of unique elements (elements from one of the columns).

I would like to leave only rows with elements in DataFrame which are included in my list. I tried to do that but all my ideas failed.

Below quick example:

list = ["a", "b"]

Col1 Col2 Col3
1    a     ok
2    b     nok
3    c     ok
4    d     ok
5    a     nok

So I want to keep only rows where are A and B (elements from my list).

CodePudding user response:

Try this:

df.loc[df[column_you_want].isin(your_list)]

CodePudding user response:

df = df.loc[df["Col2"].isin(["a", "b"])]

will give:

Col1 Col2 Col3
1    a     ok
2    b     nok
5    a     nok
  • Related