0

I have certain rows that I want to drop from the df1. I did write the conditions this way and showed me the exact rows that I wanted to delete. However, when I try to apply drop on this data, it doesn't work :

to_be deleted = df1.loc[df1['barcode'].str.contains('....-..-....-11.', regex=True)] 

when I use

to_be deleted.head() print(len(to_be deleted)) 

I can see the data that I want to delete, which means the code worked. However, when I try drop these rows, it doesn't work

df2 = df1.drop([df1['barcode'].str.contains('....-..-....-11.', regex=True)], axis=1, inplace=True) 

also I tried

df2 = df1.drop(to_be_deleted, axis=1, inplace=True) 

but it either shows :

'Series' objects are mutable, thus they cannot be hashed 

or

/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame 

How can I drop these rows I specified in (to_be_deleted) data frame?

Thank you

1
  • The second answer in the linked question might be what you are looking for . Commented Apr 14, 2018 at 11:53

1 Answer 1

0

You do not need to use pd.DataFrame.drop for this:

mask = df1['barcode'].str.contains('....-..-....-11.', regex=True) df1 = df1[~mask] 

The ~ operator represents negation. Since mask is a Boolean array, it is negated and used as a row filter on df1.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, thank you so much. It works now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.