简体   繁体   English

合并数据框不是基于索引而是值

[英]Merging Dataframes not based on index but values

I have 2 dataframes that I'd like to merge.我有 2 个要合并的数据框。

The first df summarizes the top 5 most common venues in each town:第一个 df 总结了每个城镇中最常见的 5 个场地:

第一自由度

The second df summarizes the frequencies of each venue category in each town:第二个df总结了每个城镇中每个场地类别的频率:

第二个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.我想合并两个数据框,以便前 5 个场地的频率也出现在第一个 df 中。

For eg.例如。

Output on row 0: Output 在第 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我试过 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?如果我的合并位于 1 df 的列名和另一个 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM