如何将tw0数据集与匹配索引结合起来?

问题描述 投票:0回答:1

我想合并两个具有不匹配行条目的数据集。这两个集合通过匹配编号索引来对齐。我想查看左侧数据集(数据集编号 1)以保留所有数据,但附加/连接/连接/合并数据集编号 2 中的字符串值。我还想仅返回数据集编号 1 的 2 列。我当然有很多方法可能有效。我希望了解一种直接执行此操作的方法,因为我有很多类似的案例。数据类型可能会有所不同。

example Dataset Number 1: 

      Item Name:     Item Size:
 
 0    first12345     10      
 1    second12345    10 
 2    third12345     10
 68   fourth12345    20
 88   fifth12345     20
 90   sixth12345     10
 91   seventh12345   10
 92   eighth12345    20
 ...
 
example Dataset Number 2:

        Item Name additional:

 68     789ten
 88     789ten
 92     789ten

这就是我想要的样子:

      Item Name:           Item Size:

 0    first12345           10      
 1    second12345          10 
 2    third12345           10
 68   fourth12345789ten    20
 88   fifth12345789ten     20
 90   sixth12345           10
 91   seventh12345         10
 92   eighth12345789ten    20

pandas join merge
1个回答
0
投票

假设

df1
/
df2
,你可以获得索引
intersection
并添加到位:

df1.loc[df1.index.intersection(df2.index),
        'Item Name'] += df2['Item Name additional']

已更新

df1

            Item Name  Item Size
0          first12345         10
1         second12345         10
2          third12345         10
68  fourth12345789ten         20
88   fifth12345789ten         20
90         sixth12345         10
91       seventh12345         10
92  eighth12345789ten         20
© www.soinside.com 2019 - 2024. All rights reserved.