Home > Back-end >  Python Compare Two Column
Python Compare Two Column

Time:10-15

I have the below condition in web1.xslx file

Data1 Data2
ABC CBD
CBD NCD
CBD NCN
CBE CBE
DHE DHE

I need to compare Data1 and Data2 and if both is equal then output in status as OK else NOK like below in separate excel sheet web2.xslx using Python Script

Data1 Data2 status
ABC CBD NOK
CBD NCD NOK
CBD NCN NOK
CBE CBE OK
DHE DHE OK

CodePudding user response:

Assuming your dataframe is named df, you could use:

df['status'] = df['Data1'].eq(df['Data2']).map({True: 'OK', False: 'NOK'})

CodePudding user response:

Try this code:

import pandas as pd

df = pd.read_excel('web1.xlsx')

status = []
for i in range (df.shape[0]):
    if (df.iloc[i,:][0] == df.iloc[i,:][1]):
        status.append('OK')
    else:
        status.append('NOK')

df['status'] = status
print(df)

Hope this helps your work, if it's correct please mark it as an answer. Thx

  • Related