简体   繁体   中英

How do I compare a dictionary value to column values

I have dataset called test includes columns "State", "Zip", "CITY" and I have a dictionary zipCode with zip code as key and cities as value like: I am trying to compare the dataset with the dictionary values and print any records who do not match. This is what I have so far:

for key, val in zipCode.items():
  if key in test['Zip']:
    if val == text['CITY'][key]
      test['match'] = True

I've not fully understood your problem, some details are missing. However, I'll try anyway with what I got from you: why not simply verify if the city is present in the zipCode dictionary?

for city in text['CITY'].values():
  if city in zipCode.values():
     test['match'] = True

or the zip code is present

for zipcode in text['CITY'].keys():
  if zipcode in zipCode.keys():
     test['match'] = True

What is the test dataset you mention? A pandas DataFrame? If it is then based on your code you could try something like this:

test['match'] = ''

for key, val in zipCode.items():
    for index, row in test.iterrows():
        if key in row['Zip'] and val == row['CITY']:
            row['match'] = True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM