简体   繁体   English

如何将两个不同的数据框 df1 df2 与特定列(列 w)进行比较,并从 df2 更新 df1 中匹配的行列 AD

[英]how to compare two different data frames df1 df2 with specific column ( column w) and update the matched rows column AD in df1 from df2

df1
A   B    C  D  E  F  
1   xyz  y  z  0  1
1   xab  z  z  0  1
2   xyz  x  p  1  1
3   xmn  m  q  2  1
3   xyx  n  r  3  1

df2
A   B    C  D  E  F  
1   xyz  x  z  4  1
1   xab  y  q  3  2
2   xyz  z  p  8  3
3   xmn  q  m  1  4
3   xyx  r  r  32  5

expected Output DF1预计 Output DF1

df1
A   B    C  D  E  F  
1   xyz  y  z  4  1
1   xab  z  z  4  1
2   xyz  x  p  8  1
3   xmn  m  q  3  1
3   xyx  n  r  32  1

i have tried df1.combine_first(df2) not working as expected我试过df1.combine_first(df2)没有按预期工作

Looking at your output I assume that you want to merge the datasets.查看您的 output 我假设您想要合并数据集。 . . . . ? ?

import pandas as pd

d = {'A': [1, 1, 2, 3, 3], 
     'B': ['xyz', 'xab', 'xyz', 'xmn', 'xyx'],
     'C': ['y', 'z', 'x', 'm', 'n'],
     'D': ['z', 'z', 'p', 'q', 'r'],
     'E': [0, 0, 1, 2, 3],
     'F': [1, 1, 1, 1, 1]}
df1 = pd.DataFrame(data=d)
d = {'A': [1, 1, 2, 3, 3], 
     'B': ['xyz', 'xab', 'xyz', 'xmn', 'xyx'],
     'C': ['x', 'y', 'z', 'q', 'r'],
     'D': ['z', 'q', 'p', 'm', 'r'],
     'E': [4, 3, 8, 1, 32],
     'F': [1, 2, 3, 4, 5]}
df2 = pd.DataFrame(data=d)

df1 = df1.merge(df2[['D', 'E']], how='inner', on='D')
df1['E'] = df1['E_y']
del df1['E_x'], df1['E_y']

Output Output

Out[41]: 
   A    B  C  D  F   E
0  1  xyz  y  z  1   4
1  1  xab  z  z  1   4
2  2  xyz  x  p  1   8
3  3  xmn  m  q  1   3
4  3  xyx  n  r  1  32

暂无
暂无

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

相关问题 一列 (df1) 与两列 (df2) 与 python 进行比较 - One column (df1) compare with two column(df2) with python 如果两个不同数据帧中两列的值匹配,则将df2中另一列的值复制到df1中的列 - If values from two columns in two different data frames match then copy values from another column in df2 to column in df1 根据第三个 df3 比较 df1 和 df2 列中的数据,并从 df2 最后一列获取匹配行数据的数据 - Compare data in df1 and df2 columns based on third df3 and get data for matched row data from df2 last column pandas 如何从 df2 获取 df1 的值,而 df1 和 df2 的值在列上重叠 - pandas how to get values from df2 for df1 while df1 and df2 have values overlapped on column(s) 如果 df2 中不存在列,如何将列从 df1 添加到 df2,否则什么也不做 - How to add a column from df1 to df2 if it not present in df2, else do nothing 如果df2索引中的df1索引,熊猫会更新列值 - Pandas update column value if df1 index in df2 index 如果找到匹配项,则比较两个不同数据框中的列,将电子邮件从df2复制到df1 - Compare columns in two different data frames if match found copy email from df2 to df1 如何通过匹配 df1 中与 df2 索引和列名匹配的列值来用 df1 中的数据填充 df2 - How to fill df2 with data from df1 by matching column values from df1 which match df2 index and column names 如何用来自另一个 dataframe (df2) 的信息填充 dataframe (df1) 的列? 就在 df1 和 df2 中的两列信息匹配时? - How to fill a column of a dataframe (df1) with info from another dataframe (df2)? Just when two column info matches in df1 and df2? 将 pandas 数据帧 (df1) 行值匹配到另一个数据帧 (df2) 列并更新数据帧 (Df1) 中不同列的行 - Match a pandas Data frame (df1) row value to another Data frame (df2) column and update a rows of different column in Data frame (Df1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM