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.
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.