0

如果所有行的值都为 False,如何删除一行?(这里意思是去掉'kiwi'。)

print(df.head())

>   concatenated   category    apple  banana  orange
> 0  apple_banana    two        True    True   False
> 1        apple     one        True   False   False
> 2        banana    one        False   True   False
> 3        kiwi      one        False   False  False

4

1 回答 1

1

您可以遍历 中的每一行df并比较列,如果您得到 3那么Falsedrop可以row

 for index, row in df.iterrows():
    if not row["apple"] and  not row["banana"] and not row["orange"]:
        df.drop(index=index, inplace=True)
           

也试试这个

  df1 = df[~(~(df['banana'])& ~(df['apple']) & ~(df['orange']))]  

回答你最后的表扬

  boolean_columns = ['apple', 'banana', 'orange']
  x = " & ".join('~('+x+')' for x in boolean_columns) 
  x = "~("+x+")"
  df1 = df.query(x)   

尝试这个

于 2020-07-16T11:57:49.197 回答