Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to concatenate a pandas column by a partition?

I have a pandas data frame like this:

df = pd.DataFrame({"Id": [1, 1, 1, 2, 2, 2, 2],
"Letter": [‘A’, ‘B’, ‘C’, ‘A’, ‘D’, ‘B’, ‘C’]})

How can I add a new column efficiently, "Merge" such that it concatenates all the values from the column "letter" by "Id", so the final data frame would look like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

output_df

>Solution :

You can groupby Id column then transform

df['Merge'] = df.groupby('Id').transform(lambda x: '-'.join(x))
print(df)

   Id Letter    Merge
0   1      A    A-B-C
1   1      B    A-B-C
2   1      C    A-B-C
3   2      A  A-D-B-C
4   2      D  A-D-B-C
5   2      B  A-D-B-C
6   2      C  A-D-B-C
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading