-2

I have a dataset (df) as below:

enter image description here

I want to drop rows based on condition when SKU is "abc" and packing is "1KG" & "5KG".

I have tried using following code:

df.drop( df[ (df['SKU'] == "abc") & (df['Packing'] == "10KG") & (df['Packing'] == "5KG") ].index, inplace=True)

Getting following error while trying above code:

NameError                                 Traceback (most recent call last)
<ipython-input-1-fb4743b43158> in <module>
----> 1 df.drop( df[ (df['SKU'] == "abc") & (df['Packing'] == "10KG") & (df['Packing'] == "5KG") ].index, inplace=True)

NameError: name 'df' is not defined

Any help on this will be greatly appreciated. Thanks.

Kraigolas
  • 5,121
  • 3
  • 12
  • 37
  • 1
    What is confusing about "NameError: name 'df' is not defined?" You never defined the variable df. – It_is_Chris Jul 12 '21 at 16:27
  • 1
    Are you sure your DataFrame is called `df`? Can you print `df.head()` to validate? – not_speshal Jul 12 '21 at 16:27
  • Welcome to StackOverflow! The problem here is that you have not defined `df`. Please see how to create a [mre], and [ask]. Among other things, these tutorials will tell you *not* to include an image of your dataframe. Instead, copy and paste it formatted as code or, even better, see [this answer](https://stackoverflow.com/a/20159305/11659881) on how to include a good pandas example. – Kraigolas Jul 12 '21 at 16:30
  • As the others above have mentioned, it doesn't see you have `df` defined. Further, the boolean AND operation in Python is `and`. I think `&` is a bitwise-AND operation. – Ben Y Jul 12 '21 at 17:32

1 Answers1

0

I suggest trying this:

df = df.loc[~((df['SKU'] == 'abc') & (df['packing'].isin(['1KG', '5KG']))]

The .loc is to help define the conditions and using ~ basically means 'NOT' those conditions.

Dharman
  • 30,962
  • 25
  • 85
  • 135