0

I'm trying to filter out a pandas data-frame based on some a variable list task_ids that exist in the df.

for example if task_ids = [1,5,7] then I'd want the following functionality:

df[(df.task_id = 1) & (df.task_id = 5) & (df.task_id = 7)]

How am I able to filter it out using a variable list? e.g. task_ids might be a list of size 1 or a list of size 13.

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
skidjoe
  • 493
  • 7
  • 16

1 Answers1

0

if you want to keep row with task_id in task_ids:

task_ids = [1,5,7]
df[df['task_id'].isin(task_ids)]

another way:

df.loc[(df['task_id'] == 1) | (df['task_id'] == 5) | (df['task_id'] == 7)]
biyazelnut
  • 256
  • 7