Victorbug
Victorbug

Reputation: 137

Groupby id and get each string from an id, in each diferent column

Hello I just want to group the elements by id and show each string in a separated column

Original dataframe:

id|elements|
    1|a
    1|b
    1|c
    1|d
    2|a
    2|b
    2|b
    3|a
    3|a
    3|b
    3|c
    3|c
    3|c

Desired output:

 id|column1|column2|column3|column4|column5|
    1 |a|b|c|d| | |
    2 |a|b|b|
    3 |a|a|b|c|c|c|

Any ideas? Thank you very much in advance

Upvotes: 1

Views: 149

Answers (2)

Marcus
Marcus

Reputation: 1023

Given your original data frame, you can simply do:

df.groupby('id').apply(lambda x: x['element'].to_list()).apply(pd.Series)

Output:

    0   1   2   3   4   5
id                  
1   a   b   c   d   NaN NaN
2   a   b   b   NaN NaN NaN
3   a   a   b   c   c   c  

If you do not want id to be the index, use .reset_index().

Upvotes: 1

Try this

import pandas as pd
import numpy as np
F = {'id': [1,1,1,1,2,2,2,3,3,3,3,3], 'element': ['a','b','c','d','a','b','b','a','a','b','c','c']}
df = pd.DataFrame(data = F)
df2 = df.set_index('id').stack().groupby(level=[0,1]).apply(list).unstack()
df3 = pd.DataFrame(df2["element"].to_list(), columns=['element1', 'element2','element3', 'element4','element5'])

Upvotes: 0

Related Questions