I have this DataFrame
data = [[1,'A','a'],
[1,'A','b'],
[1,'B','a'],
[2,'A','a'],
[2,'A','b'],
[2,'A','c']]
df_1 = pd.DataFrame(data = data,columns = ['id','Main','sub_steps'])
output
id Main Sub_steps
0 1 A a
1 1 A b
2 1 B a
3 2 A a
4 2 A b
5 2 A c
I want to groupby (id, Main) and still keep all rows
Desired Output
id Main Sub_steps lst
0 1 A a [a,b]
1 1 A b [a,b]
2 1 B a [a]
3 2 A a [a,b,c]
4 2 A b [a,b,c]
5 2 A c [a,b,c]
If I just do a group by with id and main and flatten the other row
df_1.groupby(['id','Main']).agg({'Sub_steps':list})
I will get this
Sub_steps
id Main
1 A [a, b]
B [a]
2 A [a, b, c]