Pandas 合并如何只保留第一个匹配行?

Pandas merge how to only keep the first match row?

我知道有一些类似的问题,但很少有人能得到正确的答案,而我的则不同。

我有 2 个数据框,你可以通过 运行 代码获取它们:

import pandas as pd
from io import StringIO

df1s = """
    contract  name   type    
    A8        S       ILC               
    A9        S       ILC               
"""
df1 = pd.read_csv(StringIO(df1s.strip()), sep='\s+')

df2s = """
     name   type              Basis 
     S       ILC              PO193            
     S       ILC              PO202            
"""
df2 = pd.read_csv(StringIO(df2s.strip()), sep='\s+')

然后我合并它们:

df_ia = df1.merge(df2, on=['name', 'type'], how='left')

df_ia

输出:

   contract name    type    Basis
0   A8      S       ILC     PO193
1   A8      S       ILC     PO202
2   A9      S       ILC     PO193
3   A9      S       ILC     PO202

如何只获取第一个匹配的行,输出应该是:

   contract name    type    Basis
0   A8      S       ILC     PO193
1   A9      S       ILC     PO193

drop_duplicates合并前:

>>> df1.merge(df2.drop_duplicates(["name", "type"]), how="left")
  contract name type  Basis
0       A8    S  ILC  PO193
1       A9    S  ILC  PO193