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?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Apr 8, 2018 at 4:15
2
  • You could look at the Python modulo operator of % Commented Apr 8, 2018 at 4:55
  • @Polygeo it will always work on shapefile (FID), not necessarily on features in database. Commented Apr 8, 2018 at 4:59

2 Answers 2

4

Use this advanced field calculator expression (Python):

i=0
def switch():
 global i
 i=1-i
 return i
*-------
switch()
answered Apr 8, 2018 at 4:58
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.

answered Apr 8, 2018 at 5:00
2
  • 3
    Why bother with advanced expression if simple !FID!%2 solves it for shapefile? Commented Apr 8, 2018 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. Commented Apr 8, 2018 at 5:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.