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!

1
  • "I've tried using .merge pandas" where is the code for that? Commented Jun 10, 2020 at 6:45

1 Answer 1

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))
Sign up to request clarification or add additional context in comments.

2 Comments

This works thanks! But I'm still confused about how lambda works... seems like it's a really useful tool

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.