In my attribute table, I have 2 columns with qualitative (categorical) data. Both of them are from different sources so I am trying to find the percent of matched data from these 2 columns. I created a new column labeled 'Matched', and wrote the following codeblock in the Field Calculator to determine the values, but I am getting an error on line 2-3. Do I have to convert them to string using str()?

def matchedRecords(x):
    x = (column.name)
    x2 = (column2.name)

    if x == x2:
        resultIs = "Matched"
        resultIs = "Unmatched"
    return resultIs

And then matchedRecords(!Matched!) in the second area of the Field Calculator. I want to determine the total matched values versus unmatched values to find the percentage. Is this the best way to do it? Thanks.

  • 2
    The simplest way is to compare the record count to the selection count when selecting all records with a match (which is a simple query requiring no table calculations). – whuber Jan 13 '14 at 18:00
  • 1
    You're right. This was much simpler. Thank you for your help. Just ran a query to select the records that are equal in both columns. – kaoscify Jan 13 '14 at 18:55

I think the logic is incorrect for what you are trying to accomplish. I believe you need to pass the first two columns in the function:

def matchedRecords(A, B):
    if A == B:
        results = "Matched"
        results = "Unmatched"
    return results

Then in the field calculator for the Matched field:

matchedRecords(!column1!, !column2!)

This is untested, but should work. Once you have the number (count) of matched vs unmatched, you can calculate a percentage.

  • This worked, as did the query method suggested by whuber. Thank you! – kaoscify Jan 13 '14 at 18:56
  • Yes, whuber's solution is much easier – Barbarossa Jan 13 '14 at 20:05

You can obtain the number of matching feature after selecting the feature where both fields are equal in the attribute table.

Selection > select by attribute, using the following SQL statement

[fieldA] = [fieldB]

enter image description here

then you can read the number of selected features at the bottom of your attribute table (see image) or when you use the selection snippet in the table of content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.