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:

And my target:

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?