Barcas
Barcas

Reputation: 29

How to remove rows that have "false" for every column in Pandas?

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

Upvotes: 1

Views: 3292

Answers (2)

psw
psw

Reputation: 11

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

Upvotes: 1

rahul
rahul

Reputation: 80

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)

Upvotes: 0

Related Questions