1

I have two data frames df1 and df2.I want to add Area column to df2 but the values of Area column should be from df1 and corresponding to Borough Column.

    df1 = pd.DataFrame(data = {'Borough':['Barking and Dagenham','Barnet','Bexley'],'Area':[13,33,23]})
    df2 = pd.DataFrame(data = {'Borough':['Barking and Dagenham','Barking and Dagenham',
                                          'Barking and Dagenham','Barking and Dagenham',
                                           'Barnet','Barnet','Barnet','Barnet',
                                         'Bexley','Bexley','Bexley'],
                              'name':['Asia Spice', 'Bekash', 'Cosmo', 'Tandoori Hut', 'Day of the Raj',
                                       'Spice Kings', 'Mill Hill Tandoori', 'India Garden', 'Yak & Yeti',
                                       'Le Spice', 'Cafe Raj']})

  • Please refer to : https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html – A DUBEY Dec 11 '20 at 03:35

1 Answers1

1

You want merge:

df2.merge(df1, on='Borough')

Result:

                 Borough                name  Area
0   Barking and Dagenham          Asia Spice    13
1   Barking and Dagenham              Bekash    13
2   Barking and Dagenham               Cosmo    13
3   Barking and Dagenham        Tandoori Hut    13
4                 Barnet      Day of the Raj    33
5                 Barnet         Spice Kings    33
6                 Barnet  Mill Hill Tandoori    33
7                 Barnet        India Garden    33
8                 Bexley          Yak & Yeti    23
9                 Bexley            Le Spice    23
10                Bexley            Cafe Raj    23
cfort
  • 2,655
  • 1
  • 19
  • 29