I've looked at other questions regarding similar topics, but I have yet found an answer. So I have a dataframe that looks as follows:
>>> df = pd.DataFrame(np.array([[1, 1, 1, 1, 2, 2, 2], [0, 0, 0, 1, 0, 0, 1], ['some text', 'other text', 'more text', 'new text', 'text sample', 'sample', 'sample text'], ['kw1, kw2', 'kw1, kw2, kw3', 'kw1', 'kw1, kw2, kw3, kw4', 'kw1', 'kw1, kw2, kw3', 'kw1, kw2']), columns=['value', 'cluster', 'text', 'keywords'])
>>> result = df.groupby(['value', 'cluster', 'text']).keywords.sum().to_frame()
>>> result =
value cluster text keywords
1 0 some text kw1, kw2
other text kw1, kw2, kw3
more text kw1
1 new text kw1, kw2, kw3, kw4
2 0 text sample kw1
sample kw1, kw2, kw3
1 sample text kw1, kw2
What I would like to do next based on my above mentioned question is to add a column with a list of lists to this existing dataframe based on the sliced columns value and cluster, instead of the text or keyword columns.
>>> summary = [['some, summary'], ['this, too, summ'], ['kws, of, summ'], ['summ, based, kw']]
I basically want to match each item of the summary list to a cluster. So it would look like this:
value cluster summary text keywords
1 0 some, summary some text kw1, kw2
other text kw1, kw2, kw3
more text kw1
1 this, too, summ new text kw1, kw2, kw3, kw4
2 0 kws, of, summ text sample kw1
sample kw1, kw2, kw3
1 summ, based, kw sample text kw1, kw2
Obviously the "simple" adding of a column to a dataframe does not work because the length of the list of lists to be added is not matching the dataframe length.
result['summary'] = summary
ValueError: Length of values (4) does not match length of index (7)
I also tried to use the following method, but it just adds together all keywords from one cluster, which is not exactly what I am looking for.
result['summary'] = result.groupby(['value', cluster']).transform('sum')
Is there any way to solve this?