[Solved] Columns and rows concatenation with a commun value in another column


You can use groupby and join to create the expected output. One way is to create a column to_join from the columns Tri_gram_sents and Value, and then agg this column:

df['to_join'] = df['Tri_gram_sents'] + ' ' + df['Value'].astype(str)
ser_output = df.groupby('sentence')['to_join'].agg(' '.join)

Or you can do everything in one line without create the column with apply:

ser_output = (df.groupby('sentence').apply(
             lambda df_g: ' '.join(df_g['Tri_gram_sents']+' '+df_g['Value'].astype(str))))

and you get ser_output:

sentence
1    (('<s>', '<s>'), 'ABC') 0.161681 (('<s>', 'ABC...
2    (('<s>', '<s>'), 'DEF') 0.111111 (('<s>', 'DEF...
...

where the first element looks as expected:

"(('<s>', '<s>'), 'ABC') 0.161681 (('<s>', 'ABC'), 'ABC') 0.472973 (('ABC', 'ABC'), 'ABC') 0.305732 (('ABC', 'ABC'), 'ABC') 0.005655 (('ABC', 'ABC'), '</s>') 0.434783 (('ABC', '</s>'), '</s>') 0.008547"

solved Columns and rows concatenation with a commun value in another column