3

I have a dataframe like this:

    id        text
    1         DM,HTN,Enlarged prostate
    2         hypertensive and on regular treatment
    2         LBP
    3         DM,HTN,Enlarged prostate

I want to combine the text of same id like this:

   id        text
   1         DM,HTN,Enlarged prostate
   2         hypertensive and on regular treatment LBP
   3         DM,HTN,Enlarged prostate

the texts of id 2 are combined. how can i acchieve this? Any help would be appreciated.

2 Answers 2

3

You can use group by function to do this

grouped_df = df.groupby("id").apply(lambda x: "%s" % ' '.join(x['text']))
Sign up to request clarification or add additional context in comments.

Comments

2

You can use groupby and apply function join. Last reset_index:

grouped_df = df.groupby("id")['text'].apply(' '.join).reset_index()
print (grouped_df)
   id                                       text
0   1                   DM,HTN,Enlarged prostate
1   2  hypertensive and on regular treatment LBP
2   3                   DM,HTN,Enlarged prostate

3 Comments

Thanks for the details! Checking on the functions
It worked perfectly. might be even better than the other answer because I got a dataframe instead of series with this code. Saved me some work, appreciate it.
ah sorry, first time posting a question

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.