简体   繁体   English

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

[英]Merge two dataframes if data of a column in first dataframe exists in any of the columns of another dataframe in python

I have two data frames I need to merge.我有两个需要合并的数据框。 The first one is:第一个是:

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

The second data frame is:第二个数据框是:

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 

My expected output will be:我预期的 output 将是:

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

I want to join these two data frames if any of the page strings in the first data frame exists in the path1 or path2 , or path3 , or path4 columns of the second data frame.如果第一个数据帧中的任何page字符串存在于第二个数据帧的path1path2path3path4列中,我想加入这两个数据帧。 Notice that page of the first data frame might be matched with path1 of the second data frame and I have a variety of situations.请注意,第一个数据帧的page可能与第二个数据帧的path1匹配,我有多种情况。

Is there a simple pythonic way?有没有简单的pythonic方式?

Let us try where with ffill create the merge key, then merge让我们尝试使用ffill where创建合并键,然后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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据第一个 dataframe 中的类似列合并两个数据框? - Merge two Dataframes based on a similar column from the First dataframe? Python Pandas 基于另一个对应数据框合并两个数据框 - Python Pandas Merge Two Dataframes Based on Another Correspondence Dataframe 按 dataframe 的列值合并两个数据帧 - Merge two dataframes groupby the column values of a dataframe 将两列不同的数据帧连接到另一个 dataframe - join two columns of different dataframes into another dataframe 如何在 Python Pandas 中合并两个数据帧,其中关键列名称不同,但想要从第二个数据帧中检索某些列? - How to merge two dataframes in Python Pandas, where key column names different, but want to retrieve SOME of the columns from second dataframe? 基于python中另一个数据框的2列过滤数据框 - Filter dataframes based on 2 columns of another dataframe in python 在Python中使用分隔符将两个DataFrame列合并到一个新列中 - Merge two DataFrame columns into a new column with delimeter in Python Python Dataframe 合并 Boolean 列数据为一列数据 - Python Dataframe Merge Boolean Columns Data into One Column Data 如何合并两个数据帧,一列是另一个数据帧中所有列的名称? - How do I merge two dataframes, one column are the names of all the columns in the other dataframe? 如何使用一列作为模式合并两个熊猫数据框并包括左数据框的列? - How to merge two pandas dataframes using a column as pattern and include columns of the left dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM