0

I have 2 CSV File which are looking like that:

first.csv (MasterFile)

Test1  10
Test2  20

second 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.

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

3
  • Will there be only zero or exactly one entries in each CSV file for Test1? Commented May 12, 2020 at 10:49
  • You just gave one line of data. Please give 5 lines of dummy data and then explain what you expect for that data as the result. Commented May 12, 2020 at 10:56
  • I have to files like: Article Definition<br/> 111 Product1<br/> 222 Product2<br/> 333 Product3<br/> 444 Product4<br/> This is my masterlist<br/> Now, let say i have another list: <br/> Article Definition<br/> 111 Product1<br/> 222 Product2<br/> 555 Product5<br/> I would like to compare the second list with my masterlist. As you can see there is the article 555 in my second List but not in my masterlist. In this case my code shoud 'break'. If alle article in my second list are available in the masterlist it should print me for example : True. Commented May 13, 2020 at 6:17

1 Answer 1

1

IIUC you need a left join with 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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.