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
CodePudding user response:
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
CodePudding user response:
We could query
:
out = df.query('origin!=destination')
Output:
origin destination
1 germany italy
2 germany spain
4 USA spain
6 Argentina Brazil