0

I have a dataframe and a list as follows.

         id     title     description
0  17810732  "nn nn."  "nnnn nnnn"
1  17810731  "mm mm."  "mmmm mmmm"
2  17810739  "ll ll."  "llll llll"
3  17810738  "jj jj."  "jjjj jjjj"

ids = [17810738, 17810731]

I want to get a dataframe that only has rows corresponding to the ids list.

So my output should be as follows.

         id     title     description
0  17810738  "jj jj."  "jjjj jjjj"
1  17810731  "mm mm."  "mmmm mmmm"

I have been using this code.

for id in ids:
   print(df.loc[df["id"] == id])

However, it only returns seperate dataframes to each id, which is not what I need.

I am happy to provide more details if needed.

EmJ
  • 4,398
  • 9
  • 44
  • 105

1 Answers1

1

The solution is in isin method, so

df[df['id'].isin(ids)]

will do the trick.

Matěj Račinský
  • 1,679
  • 1
  • 16
  • 28