[Python] Compare 2 columns in same excel sheet in pandas

Discussão em 'Python' iniciado por Stack, Julho 18, 2025.

  1. Stack

    Stack Membro Participativo

    In excel sheet i want to compare the 2 columns. The column names like file_1 and file_2. by using these 2 cols want to create the another col like 'diff' by using excel formula [countifs]. In case , if file_1 and file_2 column records matched it should be 0 (Zero) in diff column otherwise it should be 1. But, i am not as excepted result. Could please help anyone.

    here is my inputs :

    file_1 file_2
    G G
    A B
    C F
    E H
    A C
    H E


    Output Dataframe :

    file_1 file_2 diff
    G G 0
    A B 1
    C F 1
    E H 0
    A C 0
    H E 0


    Sample code :

    df = pd.read_excel('file1.xlsx')
    df1 = df[df['file_1'].isin(df['file_2'])]
    df['diff'] = df1
    print(df)

    Continue reading...

Compartilhe esta Página