[英]How to compare columns in two different excel sheets and if match found copy other other column values
我有两个 excel 表说 input.xlsx 和 output.xlsx
input.xlsx 表如下
SL_No map_id Req_ID Test_case_ID Automation_TC_ID Results
1 abc xyz test case1 NA Pass
2 vgf xxx test case2 NA fail
3 fds xxx test case3 NA
4 qew xxxx test case4 NA Pass
5 ayz wrew test case5 NA
6 dfd sdf test case6 NA fail
output.xlsx 如下
ID URL Name Results
20 0 test case1
21 0 test case2
22 0 test case3
23 0 test case4
24 0 test case5
25 0 test case6
在 input.xlsx 中,我们必须采用“Test_Case_ID”列并在 Output.xlsx 工作表中的“名称”列中搜索,如果匹配,则我们必须将“结果”列从 input.xlsx 工作表复制到 output 工作表的“结果”列。
注意:如果 Result 列有 Pass 那么它应该通过,如果它失败那么它应该复制为失败并且如果它为空那么它应该在 output xlsx 工作表中为空。
有人可以帮我在 python 中编写代码吗?我目前正在学习 python 在此先感谢!
我试过这个:
import pandas as pd
import numpy as np
# Load in the input
df1 = pd.read_excel('input.xlsx')
# Load in the output
df2 = pd.read_excel('output.xlsx')
df2['Results'] = np.where(df1['Test_Case_ID'] == df2['Name'], df1['Results'])
print df2.head()
但低于错误
文件“C:\Python27\lib\site-packages\pandas\core\ops.py”,第 1676 行,在包装器中
raise ValueError("只能比较相同标签的"
ValueError:只能比较相同标记的 Series 对象
您描述的是 SQL 外部连接。 您可以像这样在 pandas 中轻松执行此操作:
import pandas as pd
# Load in the input
df1 = pd.read_excel('input.xlsx')
# Load in the output
df2 = pd.read_excel('output.xlsx')
# Perform the join. The join only requires 'Test_case_ID' and 'Results' from the input.
df1[['Test_case_ID', 'Results']].merge(
# Since there is already a column called 'Results' in the output.xlsx file, which we don't need, I drop it here
df2.drop(['Results'], axis=1),
# We want one entry per line in the output, since the output (df2) is on the right, this is an 'right' join.
how='right',
# We are comparing 'Test_case_ID' from the left (df1)
left_on='Test_case_ID',
# and 'Name' from the right (df2).
right_on='Name'
).to_excel('result.xlsx') # And then saving the result in another excel file called 'result.xlsx'
有关合并 function 工作原理的更多详细信息,请参阅pandas 合并文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.