Alejandra
Alejandra

Reputation: 133

Delete the rows that have the same value in the columns Dataframe

I have a dataframe like this :

origin destination
germany germany
germany italy
germany spain
USA USA
USA spain
Argentina Argentina
Argentina Brazil

and I want to filter the routes that are within the same country, that is, I want to obtain the following dataframe :

origin destination
germany italy
germany spain
USA spain
Argentina Brazil

How can i do this with pandas ? I have tried deleting duplicates but it does not give me the results I want

Upvotes: 0

Views: 687

Answers (2)

user7864386
user7864386

Reputation:

We could query:

out = df.query('origin!=destination')

Output:

      origin destination
1    germany       italy
2    germany       spain
4        USA       spain
6  Argentina      Brazil

Upvotes: 0

user17242583
user17242583

Reputation:

Use a simple filter:

df = df[df['origin'] != df['destination']]

Output:

>>> df
      origin destination
1    germany       italy
2    germany       spain
4        USA       spain
6  Argentina      Brazil

Upvotes: 2

Related Questions