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– PolyGeo ♦2018年04月08日 04:55:37 +00:00Commented Apr 8, 2018 at 4:55
-
@Polygeo it will always work on shapefile (FID), not necessarily on features in database.FelixIP– FelixIP2018年04月08日 04:59:13 +00:00Commented Apr 8, 2018 at 4:59
2 Answers 2
Use this advanced field calculator expression (Python):
i=0
def switch():
global i
i=1-i
return i
*-------
switch()
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.
-
3Why bother with advanced expression if simple !FID!%2 solves it for shapefile?FelixIP– FelixIP2018年04月08日 05:02:34 +00:00Commented 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.Girl– Girl2018年04月08日 05:16:48 +00:00Commented Apr 8, 2018 at 5:16
Explore related questions
See similar questions with these tags.