0

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]
1
  • yes lol... but sometimes... it is probably the age😂😂🙈 Commented Mar 7, 2022 at 19:41

1 Answer 1

1

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']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.