Check three column condtion and delete based on condition

Question:

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

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

Asked By: Arpan Ghimire

||

Answers:

You can filter 3 columns if at least one value is not 0 – so deleted rows with all 0 in 3 columns:

test_df = df[df[['purchase_qty','other_adjustments','sold']].ne(0).any(axis=1)] 
print (test_df)
    item purchase_date  purchase_qty  purchase_price  other_adjustments  
1  50064       2022-01             0             0.0                 -5   
2  50066       2022-01             0             0.0               2979   
3  50202       2022-01             0             0.0              14673   
4  50204       2022-01             0             0.0               2538   

     sold  
1   854.0  
2     0.0  
3  1320.0  
4     0.0  
Answered By: jezrael

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))]
Answered By: Ynjxsjmh

You can try this:

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

This state: if one of these value is not 0 then keep it, otherwise, filter out.

Answered By: PySoL

This will work too!!

df = df[(df[['purchase_qty', 'other_adjustments', 'sold']] != 0).any(axis=1)]

This is a fix to your method used:

df = df.drop(df[(df.purchase_qty == 0) & (df.other_adjustments == 0) & (df.sold == 0.0)].index)

Answered By: David
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.