34

My df has 3 columns

df = pd.DataFrame({"col_1": (0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0), 
                   "col_2": (0.0, 0.24, 1.0, 0.0, 0.22, 3.11, 0.0),
                    "col_3": ("Mon", "Tue", "Thu", "Fri", "Mon", "Tue", "Thu")}) 

I want to drop rows where df.col_1 is 1.0 and df.col_2 is 0.0. So, I would get:

df = pd.DataFrame({"col_1": (0.0, 0.0, 1.0, 0.0, 1.0), 
                   "col_2": (0.0, 0.24, 1.0, 0.22, 3.11),
                    "col_3": ("Mon", "Tue", "Thu", "Mon", "Tue")})

I tried:

df_new = df.drop[df[(df['col_1'] == 1.0) & (df['col_2'] == 0.0)].index]

It gives me the error:

'method' object is not subscriptable

Any idea how to solve the above problem?

6 Answers 6

63

drop is a method, you are calling it using [], that is why it gives you:

'method' object is not subscriptable

change to () (a normal method call) and it should work:

import pandas as pd

df = pd.DataFrame({"col_1": (0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0),
                   "col_2": (0.0, 0.24, 1.0, 0.0, 0.22, 3.11, 0.0),
                   "col_3": ("Mon", "Tue", "Thu", "Fri", "Mon", "Tue", "Thu")})

df_new = df.drop(df[(df['col_1'] == 1.0) & (df['col_2'] == 0.0)].index)
print(df_new)

Output

   col_1  col_2 col_3
0    0.0   0.00   Mon
1    0.0   0.24   Tue
2    1.0   1.00   Thu
4    0.0   0.22   Mon
5    1.0   3.11   Tue
Sign up to request clarification or add additional context in comments.

2 Comments

Just to complement this answer: & is the logical AND operator, | is the logical OR and ~ is the logical NOT
Why was the .index method needed at the end? I see that it doesn't work without it.
19

Try to filter your df with loc. It's so powerful! The "~" means you want to keep those with the opposite of your condition. The ":" means you want to keep all the columns

df = df.loc[~((df['col_1'] == 1.0) & (df['col_2'] == 0.0)),:]

Comments

10

You can use or (|) operator for this , Refer this link for it pandas: multiple conditions while indexing data frame - unexpected behavior

i.e dropping rows where both conditions are met

 df = df.loc[~((df['col_1']==1) | (df['col_2']==0))]

1 Comment

The "|" operator is for "OR", while the "&" operator is for "AND" conditions. I think what is needed here is the & operator
9
mask = df['Product_Code'].isin(['filter1', 'filter2', 'filter3'])
df = df[~mask]
df.head()

.isin() allows you to filter the entire dataframe based on multiple values in a series. This is the least amount of code to write, compared to other solutions that I know of.

Adding the ~ inside the column wise filter reverses the logic of isin().

Comments

1

Put the location of row which you want remove at "location".

df = df.drop(['location' axix=1, inplace=True]

Comments

0

You can also use query here:

In [4]: df.query('~(col_1 == 1 & col_2 == 0)')
Out[4]: 
   col_1  col_2 col_3
0    0.0   0.00   Mon
1    0.0   0.24   Tue
2    1.0   1.00   Thu
4    0.0   0.22   Mon
5    1.0   3.11   Tue

With ~ the query is negated, returning those observations where the condition col_1 == 1 & col_2 == 0 does not hold.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.