简体   繁体   中英

Dropping all rows except ones with a certain criteria

I want to drop all rows that do not contain "PD-19-05-16" or "PD-19-06-01" in column "Document No." but after I ran the code it doesn't appear to make any changes.

Any advice on how to properly do this or correct the code would be greatly appreciated.

I tried using df.drop but have been running into lots of problems.

df = df.drop(['Document No.'] != ['PD-19-05-16','PD-19-06-01'])

I expected this to work but got ValueError: Arrays were different lengths: 4533 vs 2

try:

rows_to_keep = ["PD-19-05-16","PD-19-06-01"]
df.loc[df['Document No.'].isin(rows_to_keep)] 
#or if you need the inverse
df.loc[~df['Document No.'].isin(rows_to_keep)] 

I don't think that's the correct use of drop tbf. I always find it easier to use isin to filter on a single column.

the ~ acts as negatory operator

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