简体   繁体   English

比较两个数据帧并返回值

[英]Compare two data frames and return the value

I am new to programming and would like to get some suggestions on the given question.我是编程新手,想就给定问题获得一些建议。

DF_1 = r'H:\test\Output.xlsx' DF_1 = r'H:\test\Output.xlsx'

Object Object Code代码 Word单词 A一个 B C C D D E
AXe斧头 31001 31001 funktion功能 GFK GFK house 1010 1010 100 100 100 100
AY是的 31001 31001 Start开始 GFK GFK hotel酒店 1020 1020 200 200 200 200

DF_2 = r'H:\test\input.xlsx' DF_2 = r'H:\test\input.xlsx'

Obj对象 AB AB BC公元前 CD光盘 EF英孚 GH生长激素
0 0 31001 31001 0 0 0 0 0 0 1010 1010
0 0 31001 31001 0 0 0 0 0 0 1020 1020

I would like to compare these 2 excel files and append the ouput to the columns in the excel_2.我想比较这 2 个 excel 文件和 append 输出到 excel_2 中的列。

output: output:

Obj对象 AB AB BC公元前 CD光盘 EF英孚 GH生长激素
AXe斧头 31001 31001 funktion功能 GFK GFK house 1010 1010
AY是的 31001 31001 Start开始 GFK GFK hotel酒店 1020 1020

It is a matter of simple merge.这是一个简单的合并问题。 However, you'll need to rename the columns later.但是,您稍后需要重命名这些列。

out = pd.merge(df2['AB', 'GH'], df1, how='left', left_on=['AB', 'GH'], right_on=['Code', 'C'])

out.rename(columns={
    'Object': 'Obj',
    'Word': 'BC',
    'A': 'CD',
    'B': 'EF',
}, inplace=True)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM