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

Aran-Fey
  • 35,525
  • 9
  • 94
  • 135
Sam
  • 169
  • 8

1 Answers1

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.

jpp
  • 147,904
  • 31
  • 244
  • 302