Python / Pandas - Merge columns with the same index in a single cell -


i have dataframe:

                                     dude group_id                                                       820125                            armando 820125                       luis oswaldo 64907                            bernardo 64907                              sandro 64907                            veronica 

i want this:

                                        dudes group_id                                                       820125                 armando | luis oswaldo 64907            bernardo | sandro | veronica 

already tried variations of merge, join , concat wasn't successful. idea?

you can group index , join column:

df.groupby(level=0).agg(' | '.join) #                                   dude #group_id    #64907      bernardo | sandro | veronica #820125           armando | luis oswaldo 

to join specific column only, use dictionary in agg function:

df.groupby(level=0).agg({"dude": ' | '.join}) 

same output above.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -