2

I want to remove all rows where Column "a" value = Column "b" value from the DataFrame like this:

    a   b
1 AAA BBB
2 AAA CCC
3 AAA AAA
4 CCC CCC
5 CCC BBB
6 CCC DDD

Desired output:

     a  b
1 AAA BBB
2 AAA CCC
3 CCC BBB
4 CCC DDD

4 Answers 4

2
In [93]: df.loc[df.a.ne(df.b)]
Out[93]:
     a    b
1  AAA  BBB
2  AAA  CCC
5  CCC  BBB
6  CCC  DDD

keep the rows where "a" values are not equal to the "b" values.

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

Comments

2

df_filtered = df[df['a'] = df['b']]

or if you want to drop the rows then

df.drop(df[df['a'] = df['b']].index, inplace = True)

Comments

1

you can try:

df = df[df['a'] != df['b']]

Comments

-1

you can drop the duplicates like this.

df = df.drop_duplicates()

4 Comments

that does something different, you can see by running that
yeah, it drops rows by other pattern
fwiw, not my downvote but i can see why
This does not exactly answers the author's question. It will drop the duplicate rows in data frame, but questions asked was to drop rows where values in both the columns are same.

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.