Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
How to iterate and flatten each column in one groupby value

I want to firstly group different rows, if their first item in the row is the same, then flatten each columns in one group into a list.

My initial dataframe:

enter image description here

And my target:

enter image description here

The problem is that I cannot use simply df.groupby('key').apply(list) cause it will flatten columns val1 and val2 together`

Is there any way to do this?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use agg instead of apply:

df.groupby('key').agg(list)

Output:

    key       val1       val2
0  key1  [1, 2, 5]  [2, 3, 6]
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!