0

I am trying to delete column based on two conditions in a dataframe. I am trying to delete a column from a data frame and I wrote the following code

DATAFRAME= DATAFRAME.drop(DATAFRAME.loc[(DATAFRAME['COLUMN1'] ==80) & (DATAFRAME['COLUMN2'] =='86%')])

and its showing an error.

"[col1,col2,col3,col4....coln] not found in axis"

selection is working

DATAFRAME.loc[(DATAFRAME['COLUMN1'] ==80) & (DATAFRAME['COLUMN2'] =='86%')]
rpanai
  • 12,515
  • 2
  • 42
  • 64

3 Answers3

0

you can use:

DATAFRAME = DATAFRAME.loc[~((DATAFRAME['COLUMN1'] ==80) & (DATAFRAME['COLUMN2'] =='86%'))]
David
  • 871
  • 1
  • 5
  • 13
0

Since selection works as intended you can get the index of the rows meeting your criteria and use this approach. For your case it would look like this:

DATAFRAME.drop(DATAFRAME.loc[(DATAFRAME['COLUMN1'] == 80) & (DATAFRAME['COLUMN2'] == "86%")].index)
0

Why don't you use just filtering here?

import pandas as pd
DATAFRAME = pd.DataFrame(
    {"COLUMN1":[80, 100, 80],
    "COLUMN2": ['84%','85%', '86%' ]})

These are the indicies where your conditions are satisfied

cond = (DATAFRAME['COLUMN1'].eq(80) &  
     DATAFRAME['COLUMN2'].eq('86%'))
0    False
1    False
2     True
dtype: bool

So in order to drop them you can just filter

DATAFRAME[~cond]

which gives you the following output

   COLUMN1 COLUMN2
0       80     84%
1      100     85%
rpanai
  • 12,515
  • 2
  • 42
  • 64