bernando_vialli
bernando_vialli

Reputation: 1019

Joining 2 pandas dataframes on a very large number of columns

Is it possible to join two pandas dataframes on a very large number of columns in one of the dataframes? I have 15 in this hypothetical example but I would really like to do this for about 100 columns and a lot more rows...

Say you have Dataframe 1:

A     Type
123   hi
356   bye
999   nay
222   no

Dataframe 2 is:

1    2     3    4     5    6   7   8  9  10 11  12   13   14   15....
0    0    123  356   999  999 222  0  0   0  0   0    0    0    0
222  356    0    0     0  356 123 999 0   0  0   0    0    0    0

and for the output to be

1    2     3    4     5    6   7   8  9  10 11  12   13   14   15

           hi   bye   nay  nay no
no   bye                   bye hi  nay 

Upvotes: 0

Views: 82

Answers (2)

BENY
BENY

Reputation: 323226

IIUC using replace

df2.replace(df1.set_index('A').T.to_dict('r')[0]).replace(0,'')
Out[830]: 
    1    2   3    4    5    6   7    8 9 10 11 12 13 14 15
0           hi  bye  nay  nay  no                         
1  no  bye                bye  hi  nay                    

Or we can using map

d=df1.set_index('A').T.to_dict('r')[0]
df2.apply(lambda x :x.map(d)).fillna('')

Upvotes: 3

Patrick the Cat
Patrick the Cat

Reputation: 2158

Maybe this is cleaner?

df2.replace(df1.set_index('A')['Type'].to_dict())

Upvotes: 0

Related Questions