两个不同的Excel文件以匹配具有相同名称的行

时间:2018-07-18 13:04:05

标签: python pandas

使用python熊猫

我正在尝试在pandas中编写一个条件,该条件将匹配来自两个不同excel文件中具有相同列名和不同数值的两个列。每列有2000行要匹配。

条件:

  • 如果最终值=(如果File1(column1value)-File2(column1value)= 0,则将值更新为1;
  • 如果File1(column1value)-File2(column1value)小于或等于0.2,则保持File1Column1Value;
  • 如果(File1Column1)-File2(column1value)大于0.2 the。将值更新为0。

https://i.stack.imgur.com/Nx3WA.jpg

1 个答案:

答案 0 :(得分:0)

    df1 = pd.read_excel('file_name1') # get input from excel files
    df2 = pd.read_excel('file_name2')
    p1 = df1['p1'].values
    p11 = df2['p11'].values
    new_col = [] # we will store desired values here
    for i in range(len(p1)):
       if p1[i] - p11[i] == 0:
          new_col.append(1)
       elif abs(p1[i] - p11[i]) > 0.2:
          new_col.append(0)
       else:
          new_col.append(p1[i])
    df1['new_column'] = new_col # we add new column with our values

您还可以删除旧列df.drop('column', axis = 1)