Skip to content
Advertisement

Alternating column values

I am working on a project where my dataset looks like bellow:

Origin Destination Num_Trips
Hamburg Frankfurt 2
Hamburg Cologne 1
Cologne Hamburg 3
Frankfurt Hamburg 5

I am interested only on one way either “Hamburg – Frankfurt” or “Frankfurt – Hamburg” and add them as number of trips made between this two locations. How can i do this in pandas so that i have one of them in my dataset with the total number of trips made between the two points either side?

Final Table:

Origin Destination Num_Trips
Hamburg Frankfurt 7
Hamburg Cologne 4

Thanks :)

Advertisement

Answer

Here’s a simple solution to your problem –

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement