5

Lets say I have a DataFrame like following.

      A      B
0    text1   200
1    text2   200
2    text1   400
3    text2   500
4    text1   300
5    text1   600
6    text2   300

I want to print following output

     A             B
0    text1,text2   200
2    text1         400
3    text2         500
4    text1,text2   300
5    text1         600

There is no order,I just want to take text labels of column "A" for matching values in column "B". This has to be done with df.groupby as I know. Any way no success with my efforts yet. Hope you get my question.

1 Answer 1

9

You can nearly do this with a plain ol' sum:

In [11]: df.groupby('B').sum()
Out[11]:
              A
B
200  text1text2
300  text1text2
400       text1
500       text2
600       text1

You could use an aggregate with a join:

In [12]: df.groupby('B').agg(lambda x: ', '.join(x.values))
Out[12]:
                A
B
200  text1, text2
300  text1, text2
400         text1
500         text2
600         text1
Sign up to request clarification or add additional context in comments.

2 Comments

As always your answers are short sweet and elegant..:) Thanks worked perfectly.
Also, nearly a transform: df.groupby('B').transform(', '.join) (which would have been more elegant)... Glad it helped!

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.