1

I have a dataframe like the below- both columns are strings, with the ValCol being a string of comma separated integers. The index is a generic integer index with no meaning.

NameCol    ValCol
Name1      555, 333
Name2      433
Name1      999
Name3      123
Name2      533

What's the best way to aggregate it to

NameCol    ValCol
Name1      555, 333, 999
Name2      433, 533
Name3      123

T don't care about the order of the comma separated integers, but I do need to keep commas between them. It likely will be a very small dataframe, <100 records, so efficiency isn't critical.

I feel like there should be some groupby approach to this, but I haven't figured it out yet.

1 Answer 1

4

Using a groupby approach:

df = df.groupby('NameCol')['ValCol'].apply(', '.join).reset_index()

The resulting output:

  NameCol         ValCol
0   Name1  555, 333, 999
1   Name2       433, 533
2   Name3            123
Sign up to request clarification or add additional context in comments.

1 Comment

That makes so much sense and is very clean, thank you!

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.