[Python] dictionary of columns to compare in data frame

Discussão em 'Python' iniciado por Stack, Março 4, 2025.

  1. Stack

    Stack Membro Participativo

    i have a data frame with two columns (name1, name2) i would like to use a dictionary of column names and then do a for loop that compares if the values are the same and specifically show the values that are not the same

    when i try the following i get an error "ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()"

    # create df
    test2 = {'NAME1': ['Tom', 'nick', 'krish', 'jack'],
    'NAME': ['Tom', 'nick', 'Carl', 'Bob']}
    dfx = pd.DataFrame(test2)

    #create dictionary
    thisdict = {
    "NAME1": "NAME"
    }

    #loop and display differences
    for a, b in thisdict.items():
    if dfx[a] != dfx:
    x = dfx[[a, b]]
    print(x)

    Continue reading...

Compartilhe esta Página