Skip to content
Advertisement

How can I consolidate multiple rows into a single row based off their values in a Pandas Dataframe?

I have a dataframe called Traffic:

JavaScript

I’d like to end up with a dataframe like so:

JavaScript

Where the 4 rows are combined into 1 based off the Source. The traffic methods are then further broken up by their destinations in ascending order. If there are multiple entries from say LA->NY of type Ground, add the weights.

Ground/Air columns would be strings following the format of “Destination:TotalWeight”.

This pattern would continue all the way down for any other Sources such as ‘WA’…

Advertisement

Answer

initialize

JavaScript

do stuff

JavaScript

output

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