Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate strings based on inner join

I have two DataFrames containing the same columns; an id, a date and a str:

df1 = pd.DataFrame({'id':      ['1', '2', '3', '4', '10'], 
                    'date':    ['4', '5', '6', '7', '8'],
                    'str':     ['a', 'b', 'c', 'd', 'e']})

df2 = pd.DataFrame({'id':      ['1', '2', '3', '4', '12'], 
                    'date':    ['4', '5', '6', '7', '8'],
                    'str':     ['A', 'B', 'C', 'D', 'Q']})

I would like to join these two datasets on the id and date columns, and create a resulting column that is the concatenation of str:

df3 = pd.DataFrame({'id':      ['1',  '2',   '3',  '4', '10', '12'], 
                    'date':    ['4',  '5',   '6',  '7', '8',  '8'],
                    'str':     ['aA', 'bB', 'cC', 'dD', 'e', 'Q']})

I guess I can make an inner join and then concatenate the strings, but is there an easier way to achieve this?

like image 570
N08 Avatar asked Mar 09 '18 15:03

N08


2 Answers

IIUC concat+groupby

pd.concat([df1,df2]).groupby(['date','id']).str.sum().reset_index()
Out[9]: 
  date  id str
0    4   1  aA
1    5   2  bB
2    6   3  cC
3    7   4  dD
4    8  10   e
5    8  12   Q

And if we consider the efficiency using sum() base on level

pd.concat([df1,df2]).set_index(['date','id']).sum(level=[0,1]).reset_index()
Out[12]: 
  date  id str
0    4   1  aA
1    5   2  bB
2    6   3  cC
3    7   4  dD
4    8  10   e
5    8  12   Q
like image 121
BENY Avatar answered Oct 21 '22 09:10

BENY


Using radd:

i = df1.set_index(['date', 'id'])
j = df2.set_index(['date', 'id'])

j['str'].radd(i['str'], fill_value='').reset_index()

  date  id str
0    4   1  aA
1    5   2  bB
2    6   3  cC
3    7   4  dD
4    8  10   e
5    8  12   Q

This should be pretty fast.

like image 42
cs95 Avatar answered Oct 21 '22 08:10

cs95