mardi 8 janvier 2019

Is there a way to see if a dictionary's value's indices are equal? Python

There's more but the portion in question looks like this:

df.set_index('ID', inplace=True)
my_dict = df.to_dict(orient='index')   

for i, value in enumerate(my_dict): 
    # If Name and Number have the same index, print their values
    if (my_dict[i]['NAME']) == (my_dict[i]['NUMBER']):
        print(my_dict[value]['NAME'], my_dict[value]['NUMBER'])

Specifically, I want to know how I can get the equivalent of the IF statment. I know what I have here is wrong because I am getting a KeyError: 0 as a result. How can I do this in python?

Aucun commentaire:

Enregistrer un commentaire