Reputation: 81
I think the solution is very simple but I don't have any idea why I am so stupid today....
I have a dataframe like this
A B C
1 a red
1 a blue
1 c blue
1 c yellow
2 b red
2 d blue
3 a green
3 c yellow
3 d blue
3 d yellow
3 f red
And the result should be simple like this only a list or one column. So the former 1st column should be the headline with a line break before.
1
a [red, blue]
c [blue, yellow]
2
b [red]
d [blue]
3
a [green]
c [yellow]
d [blue, yellow]
f [red]
Upvotes: 0
Views: 820
Reputation: 483
Let df
be your DataFrame you defined above. Use pandas.DataFrame.groupby to create groups/subgroups and loop over them:
groups = df.groupby('A')
for g in groups:
print(g[0]) # "headline" of the following block
subgroups = g[1].groupby('B')
for sg in subgroups:
print(sg[1]['B'].values[0], sg[1]['C'].values)
print(' ')
This yields:
1
a ['red' 'blue']
c ['blue' 'yellow']
2
b ['red']
d ['blue']
3
a ['green']
c ['yellow']
d ['blue' 'yellow']
f ['red']
Upvotes: 1