简体   繁体   中英

Is there a better way to select rows from a pandas DataFrame based on multiple conditions?

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?

You can use query

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

See also this question.

I will do all after eq

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM