0

I have a dataframe df and want to create a new dataframe df_b from it but only taking the rows where the value of the row's column df['id'] is in my list array list_of_ids.

Both df['id'] and list_of_ids contain string values.

I thought of using a regex, but the regex would be huge since the length of list_of_ids is > 20 elements, so would need a generator over list_of_ids but I don't know how to apply that.

I was thinking something like:

list_of_ids = ["thing1", "thing2", "thing3" ]
df_b = df[df["id"].apply(lambda x: x in list_of_ids)==True]

Or I could use the .str.contains() method but pass a string that is built from all the elements of list_of_ids where they are separated by a pipe '|', but doing that doesn't seem "clean".

2
  • 2
    df[df['id'].str.isin(list_of_ids)] Commented Mar 5, 2019 at 17:54
  • That's what I needed. Thanks. I'll accept this as an answer since you got there before @perl with it, otherwise I'll accept Perl's. Commented Mar 5, 2019 at 18:23

1 Answer 1

1

Generating a sample DataFrame:

n = 50
df = pd.DataFrame({
    'id': list(string.ascii_letters[:n]),
    'n': range(n)})
df.head()

Out:
    id  n
0   a   0
1   b   1
2   c   2
3   d   3
4   e   4

Selecting values with ID matching values from the ids list:

ids = ['a', 'd', 'x', 'A']
df[df['id'].isin(ids)]

Out:
    id  n
0   a   0
3   d   3
23  x   23
26  A   26
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. That's what I needed.
@uncle-junky: if the answer solves your problem as an acceped solution, you might give an up-vote and also click the "solved-button" (just below th up-/down-voting buttons)
@pyano does it matter who got there first? I'm happy to give credit wherever depending on community rules/guidelines.
If @panktijk decides to post their solution as an answer you can decide to change to their answer as the accepted one after. However if they do not wish to post it as answer and this one solves your problem, you should accept this one as the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.