Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas drop rows on multiple conditions

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading