Sujin
Sujin

Reputation: 273

how to map with multiple data frame?

I would like to map between two data frames which are

df_list
  a b
0 1 3
1 2 1
2 3 4
3 2 4
4 3 1

and df_name

 num_l name
0 1    Mark
1 2    John
2 3    Sara
3 4    David

and then get the result like this

result
  a b  name_a name_b
0 1 3  Mark   Sara
1 2 1  John   Mark
2 3 4  Sara   David
3 2 4  John   David
4 3 1  Sara   Mark

at first, I try to create data frames that collect from 'a' and 'name' and 'b','name' then concat it together, but the order refer to df_list is not working. thank you in advance.

Upvotes: 0

Views: 165

Answers (1)

BENY
BENY

Reputation: 323326

You can using replace

df2=df.replace(dict(zip(df1.num_l,df1.name))).add_prefix('name_')
pd.concat([df,df2],1)
   a  b name_a name_b
0  1  3   Mark   Sara
1  2  1   John   Mark
2  3  4   Sara  David
3  2  4   John  David
4  3  1   Sara   Mark

Upvotes: 2

Related Questions