How to merge cells with list datatypes from different dataframes in Python Pandas?

Viewed 109

I currently am trying to merge lists together from two different dataframes using pandas. I am trying to get each cell in df1 merged all the cells in df2. So as an example I have the following two dataframes:

>>> df1
0   [This, is, a, sentence]
1   [this, is, another, sentence]

>>> df2
0   [this, is, also, a, sentence]
1   [this, too, is, a, sentence]
2   [this, is, very, different]
3   [another, sentence, is, this]
4   [not, much, of, anything]

I am trying to have the two cells in df1 merge with the cells in df2, to give the following output:

>>> df_merged
0   [this, is, also, a, sentence, This, is, a, sentence]   
1   [this, too, is, a, sentence, This, is, a, sentence]       
2   [this, is, very, different, This, is, a, sentence]       
3   [another, sentence, is, this, This, is, a, sentence]     
4   [not, much, of, anything, This, is, a, sentence]       

>>> df2_merged
0   [this, is, also, a, sentence, This, is, another, sentence]   
1   [this, too, is, a, sentence, This, is, another, sentence]     
2   [this, is, very, different, This, is, another, sentence]      
3   [another, sentence, is, this, This, is, another, sentence]   
4   [not, much, of, anything, This, is, another, sentence]     

Any ideas on how I could achieve this? They don't necessarily need to be split up into different dataframes, they could be in different columns in the same dataframe etc

1 Answers

Cross join both the dataframe, then concatenate the two lists:

df1.join(df2, how='cross', lsuffix='1', rsuffix='2').apply(lambda x: x['text2']+x['text1'], axis=1)

OUTPUT:

0          [this, is, also, a, sentence, This, is, a, sentence]
1           [this, too, is, a, sentence, This, is, a, sentence]
2            [this, is, very, different, This, is, a, sentence]
3          [another, sentence, is, this, This, is, a, sentence]
4              [not, much, of, anything, This, is, a, sentence]
5    [this, is, also, a, sentence, this, is, another, sentence]
6     [this, too, is, a, sentence, this, is, another, sentence]
7      [this, is, very, different, this, is, another, sentence]
8    [another, sentence, is, this, this, is, another, sentence]
9        [not, much, of, anything, this, is, another, sentence]
dtype: object

Data used for above code (df1, and df2 have the column text):

>>> df1
                            text
0        [This, is, a, sentence]
1  [this, is, another, sentence]

>>> df2
                            text
0  [this, is, also, a, sentence]
1   [this, too, is, a, sentence]
2    [this, is, very, different]
3  [another, sentence, is, this]
4      [not, much, of, anything]
Related