Skip to content
Advertisement

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

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement