0

I have two dataframes which are in this manner:

id     Area    Name
-------------------
1      A       abc
2      B       xyz
3      C       hi

and

group    id
-----------
a        1
a        3
b        2
c        1
c        3

And I want to add first table's information to the second table like below:

group    id    Area    Name
---------------------------
a        1     A       abc
a        3     C       hi
b        2     B       xyz     
c        1     A       abc
c        3     C       hi

Now I'm using loop, but I want to know is there any effective way to solve this problem. Thank you :)

def find_meta(id, column):
    info = left.iloc[id]
    data = info[column]

    return data

for column in left.columns:
    right[column] = right['songs'].map(lambda x :find_meta(int(x),column))

image

L_J
  • 2,351
  • 10
  • 23
  • 28
Hyelin
  • 111
  • 1
  • 8
  • [I think this is what youre looking for](https://stackoverflow.com/questions/35234012/python-pandas-merge-two-tables-without-keys-multiply-2-dataframes-with-broadc) – Ferenc Lippai Jun 04 '20 at 06:06

2 Answers2

0

I think a join would do it.

Check out the documentation, but for your example it would be something like:

df_both = left.join(right, on="id")
zarnoevic
  • 329
  • 3
  • 12
0

Something like this might work -

df2 = pd.merge(df2, df1, on='id', how='left')
df2
   group   id   Area Name
0     a    1.0    A  abc
1     a    3.0    C   hi
2     b    2.0    B  xyz
3     c    1.0    A  abc
4     c    3.0    C   hi
Sajan
  • 1,247
  • 1
  • 5
  • 13