How to flatten grouped Pandas DF columns by ID?

Question:

I have the following Pandas data frame (number of rows with the same ID are always the same):

ID  VALUE
---------
1   11
1   12
2   21
2   22
3   31
3   32

I would like to get a flattened version of it where each ID have one rows with N columns with the respective values belonging to ID in VALUE column (by sequence order) like this:

ID  v1  v2
----------
1   11  12
2   21  22
3   31  32

How can I get the desired result with Pandas?

Asked By: Fredrik

||

Answers:

Example

data = {'ID': {0: '1', 1: '1', 2: '2', 3: '2', 4: '3', 5: '3'},
        'VALUE': {0: 11, 1: 12, 2: 21, 3: 22, 4: 31, 5: 32}}
df = pd.DataFrame(data)

df

ID  VALUE
0   1   11
1   1   12
2   2   21
3   2   22
4   3   31
5   3   32

Code

out = (df.groupby('ID')['VALUE']
       .apply(lambda x: pd.Series(list(x)))
       .unstack().rename(columns=lambda x: f'v{x+1}'))

out

    v1  v2
ID      
1   11  12
2   21  22
3   31  32
Answered By: Panda Kim

i hope that help you :

df["tmp"] = df.groupby("ID").cumcount() + 1
df = df.pivot(index="ID", columns="tmp").reset_index()
df.columns = [f"{t}_{n}" for t, n in df.columns]

out put :

ID_  VALUE_1  VALUE_2
0   1       11       12
1   2       21       22
2   3       31       32

whatever your ID repeat the script create a new clomuns with suffix _number

Answered By: phœnix
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.