0

Looking to join two tables together (tables 1 and 2) to look like table 3. I tried using pandas join on the dataframes, but I keep getting errors for trying to join on columns with repeated values.

Ex:

Table 1

ID A B
1 A1 B1
2 A1 B1
2 A2 B2

Table 2

ID Property
1 Property 1
1 Property 2
2 Property 1
2 Property 2
2 Property 3

Table 3 (expected output)

ID A B Property
1 A1 B1 Property 1
1 A1 B1 Property 2
2 A1 B1 Property 1
2 A1 B1 Property 2
2 A1 B1 Property 3
2 A2 B2 Property 1
2 A2 B2 Property 2
2 A2 B2 Property 3
2 A3 B3 Property 1
2 A3 B3 Property 2
2 A3 B3 Property 3
Jose
  • 31
  • 5

1 Answers1

0

To merge them you can use merge method:

out = pd.merge(df1, df2, on='ID')

output:

>>>
   ID   A   B    Property
0   1  A1  B1  Property 1
1   1  A1  B1  Property 2
2   2  A1  B1  Property 1
3   2  A1  B1  Property 2
4   2  A1  B1  Property 3
5   2  A2  B2  Property 1
6   2  A2  B2  Property 2
7   2  A2  B2  Property 3
eshirvana
  • 23,227
  • 3
  • 22
  • 38