1

I'm trying to select rows from a pandas DataFrame based on multiple conditions. The code looks like this:

row = videos_train_df[
             (videos_train_df['pid1']==pid1)
            &(videos_train_df['pid2']==pid2)
            &(videos_train_df['vid'] ==vid)]

Is there any better way (in terms of code readability) to do the same thing?

3
  • 3
    pandas.pydata.org/docs/reference/api/… ? Commented Dec 23, 2021 at 21:54
  • Do you find that difficult to read? I don't, especially the way you've spaced it. I think the meaning is quite clear. Commented Dec 23, 2021 at 21:54
  • 1
    @Tim Roberts this particular example isn't too difficult to read, but df.query is a nice way to avoid reusing the videos_train_df variable name in the condition. Plus there could be 100 conditions instead of 3 conditions and then passing a string to df.query is easier to work with Commented Dec 23, 2021 at 21:57

2 Answers 2

3

You can use query

row = videos_train_df.query(
    f"pid1 == {pid1} and pid2 == {pid2} and vid == {vid}"
)

See also this question.

Sign up to request clarification or add additional context in comments.

2 Comments

That doesn't need quoting?
Thanks, this is what I was looking for
0

I will do all after eq

new = df[df[['pid1','pid2','vid']].eq([pid1,pid2,vid]).all(1)]

Comments

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.