简体   繁体   中英

Pandas filtering based on 2 different columns conditions

So lets say, I have the following dataframe.

data = pd.DataFrame({'Name': ['RACHEL', 'MONICA', 'PHOEBE', 'ROSS', 'CHANDLER', 'JOEY', 'RACHEL', 'RACHEL'],
                        
                      'Age': [30, 35, 37, 33, 34, 30, 30, 15],
                        
                      'Salary': [100000, 93000, 88000, 120000, 94000, 95000, 100000, 10],
                        
                      'Job': ['DESIGNER', 'CHEF', 'MASUS', 'PALENTOLOGY',
                              'IT', 'ARTIST', 'DESIGNER', 'CHEF']})

which gives:

Name    Age Salary  Job
RACHEL  30  100000  DESIGNER
MONICA  35  93000   CHEF
PHOEBE  37  88000   MASUS
ROSS    33  120000  PALENTOLOGY
CHANDLER    34  94000   IT
JOEY    30  95000   ARTIST
RACHEL  30  100000  DESIGNER
RACHEL  15  10  CHEF

What I want to do it pretty simple, I want to filter(get rows) and get rows where Name != 'RACHEL' and Job;= 'CHEF';

Expected result set:

Name    Age Salary  Job
RACHEL  30  100000  DESIGNER
MONICA  35  93000   CHEF
PHOEBE  37  88000   MASUS
ROSS    33  120000  PALENTOLOGY
CHANDLER    34  94000   IT
JOEY    30  95000   ARTIST
RACHEL  30  100000  DESIGNER

Note that the last entry is removed.

What i have tried so far is:

data = data.loc[ (data.Name != 'RACHEL') & (data.Job != 'CHEF') ]

This filters other rows Where Name = "RACHEL" OR Job = "CHEF". I only want to filter the last row where Name = 'RACHEL' and in the same row the Job = "CHEF".

Any help is appreciated. Thanks.

IIUC:

#                             HERE ---v
>>> data.loc[ (data.Name != 'RACHEL') | (data.Job != 'CHEF') ]
       Name  Age  Salary          Job
0    RACHEL   30  100000     DESIGNER
1    MONICA   35   93000         CHEF
2    PHOEBE   37   88000        MASUS
3      ROSS   33  120000  PALENTOLOGY
4  CHANDLER   34   94000           IT
5      JOEY   30   95000       ARTIST
6    RACHEL   30  100000     DESIGNER

Use this:

data = data.loc[ ~((data.Name == 'RACHEL') & (data.Job == 'CHEF')) ]

You want to remove all the rows that have both Name = RACHEL and Job = CHEF . So just write that condition and invert it to filter them out.

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