从一个excel表的列中的单元格中获取值,并检查该值是否存在于另一个excel表格的列中



这是代码,我被困在这里了。我想做我在下面描述的功能代码。我需要你的帮助。谢谢

编辑于2021年11月16日:我编写了我的解决方案并与您分享:

import pandas as pd
# column selection
table_one_cols = [0, 1, 3, 4, 5, 6, 9, 17, 21]
table_two_cols = [1, 3, 4, 5, 6, 10, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
data_one = pd.read_excel('table_one.xlsx', skiprows=2, usecols=table_one_cols, index_col=0)
data_two = pd.read_excel('table_two.xlsx', usecols=table_two_cols, index_col=0)
# combine_type = inner, left, right, outer (check this types in internet)
# FristColumnName - you can write the name of column which you want to compare in both tables
df = pd.merge(data_one, data_two, on='FristColumnName', how='outer')
df = df.rename(columns={"Column 1 Old Name": "Column 2 New Name"})
print(df.columns)
# save output to new file
df.to_excel('test.xlsx')

=============================================

import openpyxl as xl
dict_store = {}
# opening the head excel file
data1 = "D:\data1.xlsx"
wb1 = xl.load_workbook(data1)
ws1 = wb1.worksheets[0]
# opening the second excel file
data2 = "D:\data2.xlsx"
wb2 = xl.load_workbook(data2)
ws2 = wb2.active
# calculate total number of rows
data1_max_row = ws1.max_row
data2_max_row = ws2.max_row
# I created this func to store the values of head table in dict (I don't know if that's right)
def create_dict():
for i in range(1, data1_max_row):
dict_store[i] = ws1.cell(i, 1).value
# for j in dict.values():
# print(j)

def check_cells():
for i in range(1, data2_max_row):
for j in dict_store.values():
if i == j:
print("true")

if __name__ == '__main__':
create_dict()
check_cells()

我有一个包含2列的表:

Column 1   Column 2
a          f
b          g
c          h
d          i
e          j

我有一个和第二个有2列的表:

Column 1   Column 2
a          a
x          b
e          c

所以,我想检查第二个表的第1列中的行是否存在于第1列的第一个表中。如果是->将另一列(第2列(的值复制到同一行的第二个表的新列3(带有一些标题名称(中。如果不是->行保持为空。

更好理解的示例:第二个表中的ae存在于表1的第1列中,但它们不在一行中。我的意思是它们是混合的。因此,现在第二个表需要添加第三列:

除此之外的结果:

Column 1   Column 2   Column 3
a          a          f (taked from column 2 in table 1)
x          b          this row stays empty, beacuse x doesn't exist there
e          c          j (taked value from column 2 in table 1)

我希望你能理解我。提前谢谢!

使用vlookup不要重新发明

最新更新