'Panda- How can some column values can be moved to new column?

I have the below data frame

d = {
 "name":["RRR","RRR","RRR","RRR","RRR","ZZZ","ZZZ","ZZZ","ZZZ","ZZZ"],
 "id":[1,1,2,2,3,2,3,3,4,4],"value":[12,13,1,44,22,21,23,53,64,9]
}

dataframe

I want the out output as below:

output



Solution 1:[1]

try this:

def func(sub: pd.DataFrame) ->pd.DataFrame:
    dfs = [g.reset_index(drop=True).rename(
        columns=lambda x: f'{x}_{n}') for n, g in sub.drop(columns='name').groupby('id')]
    return pd.concat(dfs, axis=1)

res = df.groupby('name').apply(func).droplevel(1).reset_index()
print(res)

>>>
    name    id_1  value_1  id_2  value_2  id_3  value_3  id_4   value_4
0   RRR     1.0   12.0     2.0   1.0      3.0   22.0     NaN    NaN
1   RRR     1.0   13.0     2.0   44.0     NaN   NaN      NaN    NaN
2   ZZZ     NaN   NaN      2.0   21.0     3.0   23.0     4.0    64.0
3   ZZZ     NaN   NaN      NaN   NaN      3.0   53.0     4.0    9.0

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1