Select to view content in your preferred language

python script for renaming attributes

799
4
Jump to solution
06-29-2012 02:33 AM
TilottamaGhosh
Emerging Contributor
Hi,

I am trying python scripting for the first time and couldn't achieve what I am trying to do.

So, I have five types of Landcover - Drain, Lake, River, Water Body, and Canal. I want to name all of these 'Water'.

I figured that I could do it with this small script- !LANDCOVER!.replace("River","Water") in Field Calculator.

But, I would want to do all of them together using a python script in the 'Field Calculator'. I tried with some if, else statements, but they didn't work.

Can someone please help me with this?

I will look forward to your response.

Thanks in advance,

Tilottama
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarcinGasior
Frequent Contributor
In Field Calculator select Python parser and check Show Codeblock.

Pre-Logic Script Code:
def reclass(landcover):   if landcover in ("Drain", "Lake", "River", "Water Body", "Canal"):     return "Water"   else:     return landcover


LANDCOVER =
reclass(!LANDCOVER!)

View solution in original post

0 Kudos
4 Replies
MarcinGasior
Frequent Contributor
In Field Calculator select Python parser and check Show Codeblock.

Pre-Logic Script Code:
def reclass(landcover):   if landcover in ("Drain", "Lake", "River", "Water Body", "Canal"):     return "Water"   else:     return landcover


LANDCOVER =
reclass(!LANDCOVER!)
0 Kudos
TilottamaGhosh
Emerging Contributor
Hi M,

Thanks for your reply. I tried with the code block you had sent. However, I was not able to make it work. I tried running the code selecting the 'type' as string. I am attaching a word document which shows the code as I ran it and the error in the Geoproessing result window. Could you please point out my mistake? Thanks again for your help. I will look forward to your reply.

Best Regards,

Tilo
0 Kudos
MarcinGasior
Frequent Contributor
Python is case sensitive - Reclass in not the same as reclass
Function is defined as reclass and reclass(!LANDCOVER!) should be used.

Moreover, try to use exactly the same code in Pre-Logic area as I provided.
This code replaces every occurence of "Drain", "Lake", "River", "Water Body", "Canal" to "Water"
and other values are leaved untouched.

Beware that when you make calculations on existing field LANDCOVER, its original values cannot be reverted. (Backup copy of the data may be handy).
0 Kudos
TilottamaGhosh
Emerging Contributor
Hi Marc,

Thanks a bunch! It worked! I copied and pasted the code 'as it is' and it worked.

Thanks very much.

Best Regards,

Tilottama
0 Kudos