简体   繁体   English

通过将第一个数据帧的一列与第二个数据帧的两列匹配来合并两个数据帧

[英]Merge two data frames by matching one column from the first data frame with two columns from the second data frame

I'm working with two data frames:我正在使用两个数据框:

df1 = {'Metropolitan area': {0: 'New York City',
  1: 'Los Angeles',
  2: 'San Francisco Bay Area',
  3: 'Chicago',
  4: 'Dallas–Fort Worth'},
 'token_nhl': {0: 'Devils',
  1: 'Ducks',
  2: 'Sharks',
  3: 'Blackhawks',
  4: 'Stars'}}
df2 = {'NHL': {0: 'team1', 1: 'team2', 2: 'team3', 3: 'team4', 4: 'team5'},
 'token_nhl': {0: 'Devils', 1: 'Ducks', 2: 'x', 3: 'Stars', 4: 'Sharks'},
 'token_nhl1': {0: 'a', 1: 'b', 2: 'Blackhawks', 3: 'c', 4: 'd'}}

I'm trying to merge them, but I'd like to match the values of the 'token_nhl' columns in df1 with both 'token_nhl' and 'token_nhl1' in df2, so whenever I don't find a value in 'token_nhl', I go look for it in 'token_nhl1',and then the resulting data frame would be:我正在尝试合并它们,但我想将 df1 中的“token_nhl”列的值与 df2 中的“token_nhl”和“token_nhl1”相匹配,所以每当我在“token_nhl”中找不到值时, 我 go 在 'token_nhl1' 中寻找它,然后得到的数据帧将是:

{'NHL': {0: 'team1', 1: 'team2', 2: 'team3', 3: 'team4', 4: 'team5'},
 'token_nhl_left': {0: 'Devils', 1: 'Ducks', 2: 'x', 3: 'Stars', 4: 'Sharks'},
 'token_nhl1_left': {0: 'a', 1: 'b', 2: 'Blackhawks', 3: 'c', 4: 'd'},
 'token_nhl_right': {0: 'Devils',1: 'Ducks',2: 'Blackhawks',3: 'Stars',4: 'Sharks'}}

For this you need to merge two times:为此,您需要合并两次:

1: renaming columns, becuase after merge pandas is not giving two different columns 1:重命名列,因为合并后 pandas 没有给出两个不同的列

df1 = df1.rename(columns = {"token_nhl":"token_nhl_left"})
df2 = df2.rename(columns = {"token_nhl":"token_nhl_right"})
# creating variables
left_on = "token_nhl_left"
right_on1 = "token_nhl_right"
right_on2 = "token_nhl1"
left_columns = df1.columns
  1. merge-1合并1

     df_temp1 = pd.merge(left = df1, right = df2, left_on = left_on, right_on = right_on1, how = 'left')
  2. merge-2合并 2

     df_temp2 = pd.merge(left = df_temp1[pd.isna(df_temp1[right_on1])][left_columns], right = df2, left_on = left_on, right_on = right_on2, how = 'left')
  3. concat连接

     df_final = pd.concat([df_temp1[pd.notna(df_temp1[right_on1])], df_temp2])

My approach to this question involves two steps.我解决这个问题的方法包括两个步骤。

1 - Create a chunk of code to bring the desired information to a list: 1 - 创建一段代码以将所需信息带到列表中:

lis = []
for (y,w) in zip(list(df2['token_nhl']), list(df2['token_nhl1'])):
    if y in list(df1['token_nhl']):
        lis.append(y)
    else:
        lis.append(w)

2 - Assign that list to a new data frame with all other needed data. 2 - 将该列表分配给包含所有其他所需数据的新数据框。 After that, rename columns:之后,重命名列:

df3 = df2.assign(token_nhl_right=lis)
df3.rename(columns={'token_nhl':'token_nhl_left' ,'token_nhl1':'token_nhl1_left'})

暂无
暂无

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

相关问题 如何基于一个数据框中的一列和第二个数据框中的两列合并两个数据框 - How to merge two data frames based on one column in one data frame and two column in second dataframe 比较具有不同列名的两个数据框,并使用来自第二个数据框的列更新第一个数据框 - Compare two data-frames with different column names and update first data-frame with the column from second data-frame 遍历两个数据帧,并用 pandas 中的第二个数据帧的一列更新第一个数据帧的一列 - Iterate through two data frames and update a column of the first data frame with a column of the second data frame in pandas Pandas:如何合并两个数据帧并使用第二个数据帧中的值填充 NaN 值 - Pandas: How to merge two data frames and fill NaN values using values from the second data frame 如何比较两个不同数据框中的列并保留第一个数据框中的值? - How to compare columns from two different Data Frames and keep the values from the first Data Frame? 根据两列组合的匹配将列从一个数据框中复制到另一个 - Copying column from one data frame to another based on matching of combination of two columns 如何根据 Python Pandas 中第二个数据帧中的几列合并两个数据帧? - How to merge two Data Frames based on a few columns in second Data Frame in Python Pandas? 为第一个数据帧的每一列计算两个数据帧的差异 - Calculating difference of two data frames for each column of first data frame 如果来自一列的数据存在于另一列中,则合并两个数据框 - Merge two data Frame if data from one column is present in another column 合并两个具有不同形状的数据框中的列 - Merge columns from two data frame with different shapes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM