Home > Mobile >  Deleting all rows that do not contain '1' or '8'
Deleting all rows that do not contain '1' or '8'

Time:07-16

This seems like an awfully simple question but i cannot work it out. I have a pd dataframe called 'df1' and I want to delete all rows where the 'breast_exitstat' number is not 1 or 8 - in other words the only numbers allowed in the breast_exitstat column are 1 or 8.

df1 = df1.loc[~((df1['breast_exitstat'] == 1) | (df['breast_exitstat'] == 8))]

This is currently the code I am using but it is 20 minutes into running and still hasn't finished. I do have ~75000 rows but there must be a faster way to do this- am i missing something?

CodePudding user response:

df[df['breast_exitstat'].isin([1, 8])]

CodePudding user response:

From this answer:

condition = ~((df1['breast_exitstat'] == 1) | (df['breast_exitstat'] == 8))
df1.drop(df1.loc[condition].index, inplace=True)
  • Related