Home > Software design >  Remove rows in a DataFrame that correspond to False values in a list
Remove rows in a DataFrame that correspond to False values in a list

Time:07-30

Below is a df of formulas:

          formula
0              Ac
1       Ac(AgO2)2
2       Ac(AsO2)2
3       Ac(AuO2)2
4        Ac(BO2)2
...           ...
695606     ZrZnW3
695607     ZrZnW4
695608    ZrZnWAu
695609     ZrZnWC
695610    ZrZnWSe

[694398 rows x 1 columns]

I have checked the validity of each of these formulas and have created a list called vals that is the same length as this df with True and False values corresponding to each formula. I want to remove the rows in the df that are False. Right now I have df.drop(df[vals].index, inplace = False) which outputs the False formulas but I want the True ones

CodePudding user response:

You were almost there. This should be the right syntax where : means "all columns"

df = df[vals,:]

CodePudding user response:

Are you sure the list vals indeed consists of boolen values True or False, instead of maybe their strings like 'True' or 'False'. If it is indeed a boolean list, then this should simply work

vals = [True, False, False, True, True, False, True, False, True, True, False]
df[vals]

returns

0   Ac
3   Ac(AuO2)2
4   Ac(BO2)2
6   ZrZnW3
8   ZrZnWAu
9   ZrZnWC
  • Related