1

I have a dataframe that looks like this:

    text
0   dog went to the store.    
0   cat is tall.
0   blue is red.
1   red is blue
1   blue is red

How do I concat the strings by row and group by index, so that the new df looks like this?:

    text
0   dog went to the store. cat is tall. blue is red.  
1   red is blue.blue is red

I've tried, but this is doing nothing and returns back the same number of rows, not sure where to go from here:

 df[['text']].groupby(df.index)['text'].transform(lambda x: ','.join(x))

1 Answer 1

1

A possible solution (just replace transform by agg):

df[['text']].groupby(df.index)['text'].agg(lambda x: ','.join(x))

Output:

0    dog went to the store.,cat is tall.,blue is red.
1                             red is blue,blue is red
Name: text, dtype: object
Sign up to request clarification or add additional context in comments.

2 Comments

you got it first. but same answer as above.
@RustyShackleford the other user is using a bot to generate the answers

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.