Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Map specific columns to different pandas dataframe

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    place  sale_sum lat lon
0   London    15    54  13
1   Paris     6     23  32
2   Berlin   18     13  64

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading