逐列比较两个.csv文件

2024-09-30 02:27:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图编写一个python脚本,逐列比较两个excel文件。 我只编写了一个脚本来比较文件是否相同(即相同的形状、行和列),但我在已添加的其中一个文件中有一列(即在另一个文件中不存在),我不想比较它们。我该怎么做

这就是我用来比较两个文件是否完全相同的方法

  
import pandas as pd 
  
#Reading two Excel Sheets 
  
sheet1 = pd.read_csv("crystal.csv") 
sheet2 = pd.read_csv("reformatted_crystal.csv") 
  
# Iterating the Columns Names of both Sheets 
for i,j in zip(sheet1,sheet2): 
     
    # Creating empty lists to append the columns values     
    a,b =[],[] 
  
    # Iterating the columns values 
    for m, n in zip(sheet1[i],sheet2[j]): 
  
        # Appending values in lists 
        a.append(m) 
        b.append(n) 
  
    # Iterating the list's values and comparing them 
    for m, n in zip(range(len(a)), range(len(b))): 
        if a[m] != b[n]: 
            print('Column name : \'{}\' and Row Number : {}'.format(i,m))

Tags: 文件csvthein脚本forreadzip
1条回答
网友
1楼 · 发布于 2024-09-30 02:27:29

如果您不想永久删除它,也许您可以为sheet2数据帧使用temp

import pandas as pd 
  
#Reading two Excel Sheets 
  
sheet1 = pd.read_csv("crystal.csv") 
sheet2 = pd.read_csv("reformatted_crystal.csv")
sheet2  = sheet2.loc[:,sheet1.columns ] 
  
# Iterating the Columns Names of both Sheets 
for i,j in zip(sheet1,sheet2): 
     
    # Creating empty lists to append the columns values     
    a,b =[],[] 
  
    # Iterating the columns values 
    for m, n in zip(sheet1[i],sheet2[j]): 
  
        # Appending values in lists 
        a.append(m) 
        b.append(n) 
  
    # Iterating the list's values and comparing them 
    for m, n in zip(range(len(a)), range(len(b))): 
        if a[m] != b[n]: 
            print('Column name : \'{}\' and Row Number : {}'.format(i,m))

 

相关问题 更多 >

    热门问题