Python 向数据帧添加列

Python 向数据帧添加列,python,pandas,dataframe,Python,Pandas,Dataframe,我有两个数据帧df1和df2。我想将Area列添加到df2,但Area列的值应该来自df1,并对应于Borough列 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'

我有两个数据帧df1和df2。我想将Area列添加到df2,但Area列的值应该来自df1,并对应于Borough列

    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']})


您想要合并:

df2.merge(df1, on='Borough')
结果:

                 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
请参阅: