Concatenate column values by id from another column with separator?

I have the following dataframe:

dict_df = {'sent_id': {11: 3,
  12: 3,
  24: 7,
  25: 7,
  26: 7,
  27: 7,
  28: 7,
  29: 8,
  124: 15,
  125: 15,
  126: 15,
  133: 15,
  134: 15,
  135: 15,
  357: 26,
  358: 26,
  359: 26},
 'entity': {11: 'Zhao',
  12: 'Li',
  24: 'Beijing',
  25: 'PKU',
  26: 'Chinafront',
  27: 'Technology',
  28: 'Co.,',
  29: 'Ltd.',
  124: 'January',
  125: '1,',
  126: '2006',
  133: 'December',
  134: '31,',
  135: '2006.',
  357: 'RMB',
  358: '37,560',
  359: 'Yuan'},
 'label': {11: 'B-lessor',
  12: 'I-lessor',
  24: 'B-lessee',
  25: 'I-lessee',
  26: 'I-lessee',
  27: 'I-lessee',
  28: 'I-lessee',
  29: 'I-lessee',
  124: 'B-start_date',
  125: 'I-start_date',
  126: 'I-start_date',
  133: 'B-end_date',
  134: 'I-end_date',
  135: 'I-end_date',
  357: 'B-lease_payment',
  358: 'I-lease_payment',
  359: 'I-lease_payment'}}

And I want to concatenate it back to full sentence by sent_id with " " separator.

I have tried with:

import pandas as pd

df = pd.DataFrame(df_dict)

df.groupby("sent_id").agg(" ".join) but I need to aggregate it to be:

January 1, 2006

What should I change to add separator or maybe there is a simpler method to do the same with unique values only in the label column.



Read more here: https://stackoverflow.com/questions/64404786/concatenate-column-values-by-id-from-another-column-with-separator

Content Attribution

This content was originally published by SteveS at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: