0

I have the following two dataframes:

df1:
          Value 1
0  Team_1   5
1  Team_2   8
2  Team_1   10   
3  Team_3   9
4  Team_2   3

df2:
          Value 2
0  Team_1   21
1  Team_2   32
2  Team_3   53

I'd like to join them so that I get the following:

          Value 1    Value 2
0  Team_1   5           21
1  Team_2   8           32
2  Team_1   10          21
3  Team_3   9           53
4  Team_2   3           32

I'm assuming I'll have to perform a join of some sort but I'm not sure which type I should use and whether I'd need to take any steps after that to get the desired output.

Thanks in advance!

user356
  • 303
  • 2
  • 10
  • 1
    What have you tried already? [https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html](https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html) is a good page to have a look at. – Jules SJ Jul 31 '20 at 10:52
  • `df3 = df1.copy(); df3['Value 2'] = df2['Value 2']` or `df1.join(df2)`? – Quang Hoang Jul 31 '20 at 10:53

1 Answers1

0

Given:

df1

team value_1
Team_1 5
Team_2 8
Team_1 10   
Team_3 9
Team_2 3

df2

team value_2
Team_1 21
Team_2 32
Team_3 53

Use simply pd.merge:

df1.merge(df2)

result:

     team  value_1  value_2
0  Team_1        5       21
1  Team_1       10       21
2  Team_2        8       32
3  Team_2        3       32
4  Team_3        9       53

It works because dataframes share the same column team in my case.

ipj
  • 3,488
  • 1
  • 14
  • 18