简体   繁体   English

Python 比较两个不同的 CSV 文件哪些值不在同一行中

[英]Python compare two different CSV Files which values are not in the same rows

I have 2 CSV File which are looking like that:我有 2 个 CSV 文件,看起来像这样:

first.csv (MasterFile)首先.csv (MasterFile)

Test1  10
Test2  20

second csv第二 csv

Test8  80
Test1  10

In this case i'd like to create a function which says if value (Test8) in second.csv doesn't exists in first.csv print False.在这种情况下,我想创建一个 function 表示第二个中的值(Test8)是否存在。csv 在第一个中不存在。csv 打印为假。

My Code:我的代码:

def checkContent():
    masterFile= pd.read_csv('./first.csv', error_bad_lines=False)
    df1=pd.DataFrame(masterFile, columns=[1])
    customerFile=pd.read_csv('.second.csv', error_bad_lines=False)
    df2=pd.DataFrame(customerFile, columns=[0])

    df1[1] = df2[0]
    check=np.where(df1[1]==df2[0], True, False)
    print(check) 

But it just compares the first row.但它只是比较第一行。 Not the other rows.不是其他行。 I know that it has to be done by row += 1 or something like that我知道它必须通过row += 1或类似的东西来完成

IIUC you need a left join with indicator = True IIUC你需要一个左连接, indicator = True

check = np.where(pd.merge(df2,df1,on=['A','B'],how='left'
         ,indicator=True)['_merge'] == 'left_only',
         False,
         True)

print(check)
array([False,  True])

print(pd.merge(df2,df1,on=['A','B'],how='left',indicator=True))

       A   B     _merge
0  Test8  80  left_only
1  Test1  10       both

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较两个文件中同名但值不同的两个 json 文件 - Compare two json files with same name but different values in two files 比较两个CSV文件,仅输出具有不同特定列的行 - Compare two CSV files and output only rows with the specific columns that are different Python-比较来自两个不同csv的两列中的相似值 - Python - Compare similar values in two columns from two different csv 比较两个不同长度的CSV文件找到匹配值 - Compare two CSV files of different lengths to find matching values 比较 Python 中两个不同 CSV 文件中的 select 数据 - Compare and select data in two different CSV files in Python 哪种有效的方法可以检查两个不同的excel文件/数据框中具有相同键值的多行的值? - Which is an efficient way for checking values of multiple rows with the same keyvalue in two different excel-files/dataframes? Python-迭代两个CSV文件中的每一行并比较时间戳记值 - Python - Iterate each line in two CSV files and compare timestamp values 如何比较两个文件中的行在python中是相同还是不同 - how to compare lines in two files are same or different in python 如何比较 python 中两个值是否相同但不同的情况 - how to compare if two values are the same but different cases in python 如何按关键字比较具有不同列数和行数的两个不同 CSV 文件? - How to compare two different CSV files with different number of columns and rows by keyword?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM