1

I have a lot of rows of data, and I want to use the field calculator to give a uniform assignment of

0
1
0
1
0
1
... 

but I don't know how to implement it.

Can someone help me?

  • You could look at the Python modulo operator of % – PolyGeo Apr 8 '18 at 4:55
  • @Polygeo it will always work on shapefile (FID), not necessarily on features in database. – FelixIP Apr 8 '18 at 4:59
4

Use this advanced field calculator expression (Python):

i=0
def switch():
 global i
 i=1-i
 return i
*-------
switch()
3

As @PolyGeo mentioned, the modulo operator helps you determine whether a number is pair or not. You can check the "Show Codeblock" box in the field calculator and then write a simple function that determines wheter the row is pair or not and adds a 0 or a 1 depending on the condition

def calc(fid):
    if fid % 2 != 0:
        return 0
    else:
        return 1

Then, in the box under you jsut have to call the function using the FID field as an argument.

calc( !FID! )

Note: make sure python is checked as the parser.

  • 3
    Why bother with advanced expression if simple !FID!%2 solves it for shapefile? – FelixIP Apr 8 '18 at 5:02
  • I understand this idea, but I use it to achieve, there will be mistakes.I use the previous method can be achieved. – Girl Apr 8 '18 at 5:16

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.