简体   繁体   中英

Filtering dataframe in more efficient way

How I can write following code in more pandas way:

majority_df = df[(df.voting_majority_status_fk == 4) & (df.other == True)]
minority_df = df[(df.voting_majority_status_fk == 3)]

I need to take only vp_fk that are in majority_df and not in minority_df and then take only unique rows from majority_df by found unique vp_fk

How I can write following more Pandas way.

majority_vp_fk = set(majority_df.vp_fk)
minority_vp_fk = set(minority_df.vp_fk)

clean_majority_vp_fk = majority_vp_fk - minority_vp_fk

clean_majority_df = majority_df[majority_df.vp_fk.isin(clean_majority_vp_fk)]
clean_majority_df = clean_majority_df.drop_duplicates(subset=['probe_fk', 'vp_fk', 'masking_box_fk', 'product_fk'])

Here is my "very theoretic" (it's hard to test it without sample data sets) solution:

minority_df = df[(df.voting_majority_status_fk == 3)]
qry = "voting_majority_status_fk == 4 and other == True and vp_fk not in @minority_df.vp_fk"
result = (df.query(qry)
            .drop_duplicates(subset=['probe_fk', 'vp_fk', 'masking_box_fk', 'product_fk']))

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