1

Im sure this has been asked before but i cannot phrase it in a way such that I find a similar question. What I would like to do is drop rows of my dataframe where rows with the same item name that all have value of 0 are dropped.

For example. One of my columns is the 'itemname' and the other is the 'value'. the 'itemname' may be repeated many times. I want to check for each 'itemname', if all other items with the same name have value 0, then drop these rows

I know this should be simple, however I cannot get my head around it.

Just to make it clearer, here

    itemname value
0      a       0
1      b       100
2      c       0
3      a       0
4      b       75
5      c       90

As all a items have a value of 0, They should be dropped.

    itemname value
1      b       100
2      c       0
3      b       75
4      c       90

Hope that makes sense. I check if someone else has asked something similar and couldnt find something in this case.

Callum Clark
  • 79
  • 1
  • 7

2 Answers2

1

Use:

df = df[df.groupby('itemname')['value'].transform('any')]
print (df)
  itemname  value
1        b    100
2        c      0
4        b     75
5        c     90
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

Using isin

df[df.itemname.isin(df.loc[df.value!=0,'itemname'])]
Out[543]: 
  itemname  value
1        b    100
2        c      0
4        b     75
5        c     90
BENY
  • 317,841
  • 20
  • 164
  • 234