0

I have 2 dataframes that I'd like to merge.

The first df summarizes the top 5 most common venues in each town:

1st df

The second df summarizes the frequencies of each venue category in each town:

2nd df

I'd like to merge both dataframes so that the frequency of each of the top 5 venues would also appear in the first df.

For eg.

Output on row 0:

Ang Mo Kio | Food Court | Coffee Shop | Dessert Shop | Chinese Restaurant | Jap Restaurant | 0.64 | 0.2 | 0.1 | ....

I've tried using .merge pandas

sg_venues_sorted.merge(sg_onehot_grouped, on='Town')

but that seems to be only for merging on index or column names. What if my merge is on column names of 1 df, and values of the other df?

Thanks!

sumedhe
  • 934
  • 1
  • 13
  • 30
lyh198
  • 61
  • 1
  • 6

1 Answers1

1

I think you can do this without merge. A row wise operation like this

    import pandas as pd
    df1 = pd.DataFrame({"Town":['t1','t2','t3','t4','t5'],
                       "1stcommon":["c1","c2","c3","c4","c5"],
                       "2ndcommon":["c3","c8","c1","c9","c10"]})

    df2 = pd.DataFrame({"Town":['t1','t2','t3','t4','t5'],
                       "c1":[0,0.1,0.1,0.2,0],
                       "c2":[0,0.1,0.1,0.2,0],
                       "c3":[0,0.1,0.1,0.2,0],
                       "c4":[0,0.1,0.1,0.2,0],
                       "c5":[0,0.1,0.1,0.2,0],
                       "c6":[0,0.1,0.1,0.2,0],
                       "c7":[0,0.1,0.1,0.2,0],
                       "c81":[0,0.1,0.1,0.2,0],
                       "c9":[0,0.1,0.1,0.2,0],
                        "c10":[0,0.1,0.1,0.2,0]})

    def create_col(x):
        return df2.loc[df2.Town==x['Town'],x[['1stcommon','2ndcommon']]].values[0]

    df1['1st_common'],df1['2nd_common'] = zip(*df1.apply(lambda x: create_col(x),axis=1))
sau
  • 1,316
  • 4
  • 16
  • 37
  • This works thanks! But I'm still confused about how lambda works... seems like it's a really useful tool – lyh198 Jun 10 '20 at 12:34
  • https://towardsdatascience.com/apply-and-lambda-usage-in-pandas-b13a1ea037f7 – sau Jun 11 '20 at 13:42