0

I want to keep all rows where the value in one of the columns is in a list. (or if you want delete the rows where the value in a columns is not in the list.

In the df['ID'] column there is a large number of ID's and they occur several times. There are only some of them that i want to keep. Those ID's are kept as strings in a list called surv3.

I tried this:

df = df.loc[(df['ID'] in surv3)]

aurora
  • 1

1 Answers1

0

Use the isin method.

For instance:

df = pd.DataFrame({'test': ['ga', 'bu', 'zo', 'meu']})
mylist = ['ga', 'meu']

df[df.test.isin(mylist)] returns:

test
0   ga
3   meu
Sheldon
  • 4,084
  • 3
  • 20
  • 41