使用python中的for循环将一个excel文件中的一个单元格与另一个excel文件中的一列单元格进行比较

使用python中的for循环将一个excel文件中的一个单元格与另一个excel文件中的一列单元格进行比较,python,excel,pandas,dataframe,for-loop,Python,Excel,Pandas,Dataframe,For Loop,我将使用2个excel文件。两个excel文件的文件路径都传递到函数中。传递到函数for excel file1=“Test”的文件路径。传递到excel file2=“Report”函数中的文件路径。函数为输出文件创建工作簿,并将要填充的工作表命名为“列表” 我想创建两个数据帧,df1用于excel文件1,df2用于excel文件2 我想将列“Name”及其值从excel文件1放入df1。(只是一个小例子) 我想把excelfile2中的“Name”和“State”列及其值放入df2。(只是一

我将使用2个excel文件。两个excel文件的文件路径都传递到函数中。传递到函数for excel file1=“Test”的文件路径。传递到excel file2=“Report”函数中的文件路径。函数为输出文件创建工作簿,并将要填充的工作表命名为“列表”

我想创建两个数据帧,df1用于excel文件1,df2用于excel文件2

我想将列“Name”及其值从excel文件1放入df1。(只是一个小例子)

我想把excelfile2中的“Name”和“State”列及其值放入df2。(只是一个小例子,df2更大)

我想将df1(cv1)中“名称”列下第一行的单元格与df2(cv2)中“名称”列第一行的单元格进行比较

如果cv1==cv2,我想从df2复制同一行“State”列下的单元格,并将其写入函数创建的工作簿的工作表“List”的“Best”列

否则增加df2行,重复比较

我想对df1中“name”列中有名称的所有行执行此操作

输出应该如下所示

     Best
 0   VA
 1   DC
 2   IN
 3   PA
 4   NC
下面是我创建数据帧的代码。我不知道如何使用for循环进行比较。非常感谢您的任何帮助

import os
import pandas as pd

def page(Test,Report):
       # select columns i want to work with      
       compare_column1 = ["Name"] 
   compare_column2 = ["Name", "State"]
   write_column = ["Best"]

  # create dataframes for the column to copy to the output file and columns to compare
  Df1 = pd.DataFrame(columns=compare_column1)
  Df2 = pd.DataFrame(columns=compare_column2)

 # compare cell for every row under "Name" column from df1(cv1) to every cell under “Name”
 # column df2, if the 2 cells are equal, then copy the cell under the column “State” of that row
 # of df2 and write it to the column “Best” of the workbook with the sheet name “List” the
 # function created
 # else increment the row for df2, compare cv1 to cv2…. Do this for all names in the “Name”
 # column
 # df1
 df_file2 = pd.read_excel(Test)
 df_file3 = pd.read_csv(Report)
 for i in range (0, length(df1):
    cv1 = df1.loc[i], compare_column1]
 for j in range (0, length(df2):
    cv2 = df2.loc[j], compare_column1] #not sure how to select “Name” column
    if cv1==cv2:
       cv1.to_excel(writer, sheet_name=write_column, header=false, index=false, startrow=1)

如果要使用熊猫,请不要循环。您可以使用
合并
。我无法进行测试,但假设您没有在索引上设置任何内容,请尝试:

Df1 = (Df1.reset_index()
          .merge(Df2.reset_index()[['index','Name','State']],
              how='left',
              on=['index', 'Name'])
         .drop('index', axis=1))
Df1

您好,请格式化您的代码。请参阅:我不确定如何使用for循环进行比较。非常感谢您的任何帮助。你能说得更具体些吗?请参阅,.@user106591请单击我的答案旁边的复选标记,接受其中一个答案作为解决方案。非常感谢。
import os
import pandas as pd

def page(Test,Report):
       # select columns i want to work with      
       compare_column1 = ["Name"] 
   compare_column2 = ["Name", "State"]
   write_column = ["Best"]

  # create dataframes for the column to copy to the output file and columns to compare
  Df1 = pd.DataFrame(columns=compare_column1)
  Df2 = pd.DataFrame(columns=compare_column2)

 # compare cell for every row under "Name" column from df1(cv1) to every cell under “Name”
 # column df2, if the 2 cells are equal, then copy the cell under the column “State” of that row
 # of df2 and write it to the column “Best” of the workbook with the sheet name “List” the
 # function created
 # else increment the row for df2, compare cv1 to cv2…. Do this for all names in the “Name”
 # column
 # df1
 df_file2 = pd.read_excel(Test)
 df_file3 = pd.read_csv(Report)
 for i in range (0, length(df1):
    cv1 = df1.loc[i], compare_column1]
 for j in range (0, length(df2):
    cv2 = df2.loc[j], compare_column1] #not sure how to select “Name” column
    if cv1==cv2:
       cv1.to_excel(writer, sheet_name=write_column, header=false, index=false, startrow=1)
Df1 = (Df1.reset_index()
          .merge(Df2.reset_index()[['index','Name','State']],
              how='left',
              on=['index', 'Name'])
         .drop('index', axis=1))
Df1