Home > Software engineering >  Pandas drop rows on multiple conditions
Pandas drop rows on multiple conditions

Time:05-10

I have a dataframe:

from_dict = {'weekday': {1937: 'Thursday',
                    1938: 'Thursday',
                    1939: 'Thursday',
                    1940: 'Friday',
                    1941: 'Friday',
                    1942: 'Friday',
                    1943: 'Saturday'},
        'sort': {1937: 'pre',
                 1938: 'day',
                 1939: 'twi',
                 1940: 'pre',
                 1941: 'day',
                 1942: 'twi',
                 1943: 'pre'},
        'volume': {1937: 48840,
                   1938: 43936,
                   1939: 48393,
                   1940: 47675,
                   1941: 42271,
                   1942: 46270,
                   1943: 28721}}
df = pd.DataFrame.from_dict(from_dict)
       weekday sort  volume
1937  Thursday  pre   48840
1938  Thursday  day   43936
1939  Thursday  twi   48393
1940    Friday  pre   47675
1941    Friday  day   42271
1942    Friday  twi   46270
1943  Saturday  pre   28721

I only want to slice out rows where 'weekday' == 'Friday' & 'sort' == 'day' and rows where 'weekday' == 'Thursday' & 'sort' == 'day'

       weekday sort  volume
1937  Thursday  pre   48840
1939  Thursday  twi   48393
1940    Friday  pre   47675
1942    Friday  twi   46270
1943  Saturday  pre   28721

The reason I'd prefer to slice rows is because this data frame is being passed to a pivot_table and I don't care for creating a new variable or modifying the original data frame. I've tried to slice by multiple conditions but obviously it didn't work.

sort_df[((sort_df['weekday'] != 'Thursday') & (sort_df['sort'] != 'day'))]

This just removes all rows of Thursday and day. I know the answer is going to be super obvious and I should definitely know it by now.

CodePudding user response:

Try:

>>> df[~(df["weekday"].isin(["Thursday", "Friday"])&df["sort"].eq("day"))]
       weekday sort  volume
1937  Thursday  pre   48840
1939  Thursday  twi   48393
1940    Friday  pre   47675
1942    Friday  twi   46270
1943  Saturday  pre   28721
  • Related