I have 2 dataframes that I'd like to merge.
The first df summarizes the top 5 most common venues in each town:
The second df summarizes the frequencies of each venue category in each town:
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!
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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.