首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果第一个dataframe中的列的数据存在于python中另一个dataframe的任何列中,则合并两个dataframe。

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

Stack Overflow用户
提问于 2020-08-07 22:19:59
回答 1查看 280关注 0票数 1

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

代码语言:javascript
复制
page            value
shoes           554
sneakers        226
sandals         114
boots           821
T-shirt         213
mobile-phone    284
laptop          361

第二个数据帧是:

代码语言:javascript
复制
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 

我的预期输出将是:

代码语言:javascript
复制
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匹配,我有多种情况。

有没有一种简单的pythonic方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-07 22:30:33

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

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63303693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档