1

I see a lot of questions related to dropping rows that have a certain value in a column, or dropping the entirety of columns, but pretend we have a Pandas Dataframe like the one below.

In this case, how could one write a line to go through the CSV, and drop all rows like 2 and 4? Thank you.

enter image description here

Barcas
  • 29
  • 1
  • 2
  • You apply the `all` operator to the vector of rows. Perhaps more readable would be `not any(entire row)` – Prune Jul 04 '20 at 17:15
  • Also, please note that posting `images` of code, results, etc. is not acceptable. – Prune Jul 04 '20 at 20:25

2 Answers2

1

You could try

~((~df).all(axis=1))

to get the rows that you want to keep/drop. To get the dataframe with just those rows, you would use

df = df[~((~df).all(axis=1))]

A more detailed explanation is here:

Delete rows from a pandas DataFrame based on a conditional expression involving len(string) giving KeyError

psw
  • 11
  • 3
0

This should help

for i in range(df.shape[0]):
    value=df.shape[1]
    count=0
    for column_name in column_names:
        if df.loc[[i]].column_name==False:
            count=count+1
    if count==value:
        df.drop(index=i,inplace=True)
rahul
  • 70
  • 9