Skip to content
Advertisement

Pandas – Combine multiple group rows into one row

I have been banging my head against a wall for a while now trying to figure out this seemingly easy data manipulation task in Pandas, however I have had no success figuring out how to do it or googling a sufficient answer :(

All I want to do is take the table on the left of the snip below (will be a pandas dataframe) and convert it into the table on the right (to become another pandas dataframe).

enter image description here

Code for creating the initial dataframe:

JavaScript

Thank you for your help in advance!

Advertisement

Answer

try this,

JavaScript

O/P:

JavaScript

Complete solution:

JavaScript

O/P:

JavaScript
  • What you need is groupby and aggregation ops of first and last
  • set column names
  • reset index and re order columns
Advertisement