1

I have a csv table that looks like this

    pixIndex       X       Y    R    G    B
           1       0       0  227  227  227
           2       1       0  237  237  237
           3       2       0   0     0    0
           4       3       0  232  232  232
           5       4       0  233  233  233
...        ...     ...     ...  ...  ...  ...

I would like to remove those rows that have black points i.e 0,0,0.

Currently I have this code:

import pandas as pd
df = pd.read_csv("outputdata.txt", header=None, names =["pixIndex","X","Y","R","G","B"] )
print(df)  
df = df.drop(df.query('R==0 & G==0 & B==0'))
print(df)

However at the line where I do the df.drop() call, I get an error saying

 "pixIndex","X","Y","R","G","B" not found in axis"

I have looked at these 2 SO questions but still unable to fix my code.
delete specific rows from csv using pandas
pandas: multiple conditions while indexing data frame - unexpected behavior

Why is this happening and how do I fix it?

Any help is appreciated please. Thank you! :)

Megan Darcy
  • 530
  • 5
  • 15

1 Answers1

1

With any:

df[df[['R','G','B']].ne(0).any(1)]

With query:

df.query('R!=0 & G!=0 & B!=0')

With your version of query and df.drop with additional df.index for the drop:

df.drop(df.query('R==0 & G==0 & B==0').index)
anky
  • 74,114
  • 11
  • 41
  • 70