如果python中第一个数据帧中的一列的数据存在于另一个数据帧的任何列中,则合并两个数据帧

2024-04-29 14:10:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个数据帧需要合并。第一个是:

page            value
shoes           554
sneakers        226
sandals         114
boots           821
T-shirt         213
mobile-phone    284
laptop          361

第二个数据帧是:

path1            path2            path3              path4
fashion          footwear         shoes-and-other    shoes
fashion          footwear         shoes-and-other    sneakers
fashion          footwear         sandals            NaN
fashion          footwear         shirts             T-shirt
electronic       devices          mobile-and-tablet  mobile-phone 
electronic       devices          laptop             NaN 

我的预期产出将是:

path1        path2      path3              path4        page         value
fashion      footwear   shoes-and-other    shoes        shoes        554
fashion      footwear   shoes-and-other    sneakers     sneakers     226
fashion      footwear   sandals            NaN          sandals      114
fashion      footwear   shirts             T-shirt      T-shirt      213
electronic   devices    mobile-and-tablet  mobile-phone mobile-phone 284 
electronic   devices    laptop             NaN          laptop       361

如果第一个数据帧中的任何page字符串存在于第二个数据帧的path1path2path3path4列中,我想连接这两个数据帧。请注意,第一个数据帧的page可能与第二个数据帧的path1匹配,我有多种情况

有一种简单的肾盂疗法吗


Tags: and数据pagephonenanmobileothershirt
1条回答
网友
1楼 · 发布于 2024-04-29 14:10:40

让我们尝试where使用ffill创建合并键,然后merge

df1['page'] = df1.where(df1.isin(df.page.tolist())).ffill(1).iloc[:,-1]
df1 = df1.merge(df, how='left')
df1
Out[131]: 
        path1     path2              path3         path4          page  value
0     fashion  footwear    shoes-and-other         shoes         shoes    554
1     fashion  footwear    shoes-and-other      sneakers      sneakers    226
2     fashion  footwear            sandals           NaN       sandals    114
3     fashion  footwear             shirts       T-shirt       T-shirt    213
4  electronic   devices  mobile-and-tablet  mobile-phone  mobile-phone    284
5  electronic   devices             laptop           NaN        laptop    361

相关问题 更多 >