简体   繁体   English

根据条件删除Panadas数据框的行

[英]Dropping rows of panadas dataframe based on conditional

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. 我想做的是删除数据框的行,其中具有相同项目名称且所有值均为0的行都将被删除。

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 我想检查每个“商品名称”,如果所有其他相同名称的商品的值都为0,则删除这些行

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. 由于所有项目的值为0,因此应将其删除。

    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 使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM