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

Check three column condtion and delete based on condition

I have following dataframe df in pandas

    item    purchase_date   purchase_qty    purchase_price  other_adjustments     sold
0   0040030     2022-01     0   0.00             0                0.0
1   0050064     2022-01     0   0.00            -5                854.0
2   0050066     2022-01     0   0.00            2979                  0.0
3   0050202     2022-01     0   0.00            14673                 1320.0
4   0050204     2022-01     0   0.00            2538                  0.0

I need to delete rows if all purchase_qty,other_adjustments and sold is 0.

I tried this

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

test_df = df[(df['purchase_qty'] != 0) & (df['other_adjustments'] != 0) & (df['sold'] != 0)]

This code delete all purchase_qty where it’s value is 0 but what I want is to check those 3 column and if all three are 0 then delete. Please help me

>Solution :

You need use or condition

test_df = df[(df['purchase_qty'] != 0) | (df['other_adjustments'] != 0) | (df['sold'] != 0)]

Or you can do an inverse operation

test_df = df[~((df['purchase_qty'] == 0) & (df['other_adjustments'] == 0) & (df['sold'] == 0))]
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