我想合并2个数据框:

df1:

                                              cik0        cik1        cik2
'MKTG, INC.'                            0001019056        None        None
1 800 FLOWERS COM INC                   0001104659  0001437749        None
11 GOOD ENERGY INC                      0000930413        None        None
1347 CAPITAL CORP                       0001144204        None        None
1347 PROPERTY INSURANCE HOLDINGS, INC.  0001387131        None        None

df2:
              cik Ticker
0      0001144204   AABB
1      0001019056      A
2      0001387131   AABC
3      0001437749     AA
4      0000930413  AAACU

预期结果:
                                              cik0        cik1  cik2 ticker
'MKTG, INC.'                            0001019056        None  None      A
1 800 FLOWERS COM INC                   0001104659  0001437749  None     AA
11 GOOD ENERGY INC                      0000930413        None  None  AAACU
1347 CAPITAL CORP                       0001144204        None  None   AABB
1347 PROPERTY INSURANCE HOLDINGS, INC.  0001387131        None  None   AABC

我想将cik0df2['cik']匹配,
如果不起作用,我想看看cik1,依此类推。

谢谢你的帮助!

最佳答案

您可以将 pd.Series.map fillna 一起使用几次:

ticker_map = df2.set_index('cik')['Ticker']

df1['ticker'] = df1['cik0'].map(ticker_map)\
                           .fillna(df1['cik1'].map(ticker_map))\
                           .fillna(df1['cik2'].map(ticker_map))

但是,这有点乏味。您可以定义一个函数来迭代执行此操作:
def apply_map_on_cols(df, cols, mapper):
    s = df[cols[0]].map(mapper)
    for col in cols[1:]:
        s = s.fillna(df[col].map(mapper))
    return s

df1['ticker'] = df.pipe(apply_map_on_cols,
                        cols=[f'cik{i}' for i in range(3)],
                        mapper=df2.set_index('cik')['Ticker'])

关于python - 合并到一列或另一列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54310497/

10-13 08:36