0

I have a dataframe df1 with latitude and longitude.

df1 = pd.DataFrame({
     'place': ["London", "Paris", "Berlin", "London", "Berlin"],
     'sale': [12, 6, 4, 3, 14],
     'lat': [54, 23, 13, 54, 13],
     'lon': [13, 32, 64, 13, 64],
     })
df1

    place  sale lat lon
0   London  12  54  13
1   Paris   6   23  32
2   Berlin  4   13  64
3   London  3   54  13
4   Berlin  14  13  64

After aggregating sale I get a second dataframe:

df2 = pd.DataFrame({
     'place': ["London", "Paris", "Berlin"],
     'sale_sum': [15, 6, 18],
     })
df2

   place  sale_sum
0   London  15
1   Paris   6
2   Berlin  18

Now I want to append the lat and lon to the second dataframe. Result would be like this:

    place  sale_sum lat lon
0   London    15    54  13
1   Paris     6     23  32
2   Berlin   18     13  64
PParker
  • 1,419
  • 2
  • 10
  • 25

2 Answers2

1

try this:

a = df2.merge(df1[['place','lat', 'lon']], on='place')
a.loc[~a.duplicated()]
>place  sale_sum    lat lon
0   London  15  54  13
2   Paris   6   23  32
3   Berlin  18  13  64
bpfrd
  • 945
  • 3
  • 11
0

Use pandas merge():

pd.merge(df1, df2, how='inner', on='place')
svfat
  • 3,273
  • 1
  • 15
  • 34
  • 2
    If you output this, there are duplicate rows because of the original `sale` column. This modified version of your answer give what I think OP askes for `pd.merge(df1.drop('sale', axis=1), df2, how='inner', on='place').drop_duplicates()` – Nicholas Hansen-Feruch Jul 13 '22 at 12:13