Replacing cell sentence by a different sentence with if statements

903
4
01-25-2023 01:20 AM
MarcEsmeijer
New Contributor II

Dear all,

I am trying to replace the values within cells (which in fact are short sentences) by new values (which are also short sentences) as can be seen in the figure. My desire is to replace these old values by looping through each row on the basis of if statements. Therefore I have written a small code which can be seen in the figure below.

Code_1.PNG

I started by defining a function with parameter called 'X' which in fact is a column within the same attribute table (also same length) called 'BAGGERBEHANDELING'. This column originates from a different shapefile called 'Waterlopen_W23HR04' but was added using 'Add join'.

Then I have tried to use if statements when looping through the rows of X. However this would result in a TypeError: "string indices must be integers". Therefore I have written the line: Str_X = int(X). This however does not fix the problem as I get a Value Error: "invalid literal for int() with base 10: 'op de kant' ". Now I am stuck and in need for help. How can I solve a Value Error and am I fundamentally performing a wrong method? 

I had previously tried to use the Regex package which resulted in a much more elegant code which is shown in the second figure. However I suggest that this function does not loop through each row but that it loops on the basis of values from the column that was used to perform 'Add join'. It would be nicer if the second code would loop through the individual rows, however I will focus only on the code from the first figure.

Code_2.PNG

0 Kudos
4 Replies
JohannesLindner
MVP Frequent Contributor

Well, the ValueError is absolutely expected. int(some_text) only works if some_text contains only digits, like int("404").

 

[...] looping through the rows of X [...]

Just to be clear: The Field Calculation tool takes your function, loops through the rows of the feature class, and applies the function to each row. You don't need to do the looping yourself.

Or do you mean that your text field can have multiple lines?

 

If your table looks like this, use the expression below:

TextField
links op de kant
rechts op de kant
niet op de kant
def func(x):
    replace_dict = {
        "links op de kant": "Enkelzijdige belemmering",
        "rechts op de kant": "Enkelzijdige belemmering",
    }
    return replace_dict.get(x, x)

 

 

If your table looks like this, use this expression instead:

TextField
links op de kant
rechts op de kant
op de kant
links op de kant
niet op de kant
op de kant
None
def func(x):
    replace_dict = {
        "links op de kant": "Enkelzijdige belemmering",
        "rechts op de kant": "Enkelzijdige belemmering",
    }
    lines = x.split("\n")
    new_lines = [replace_dict.get(line, line) for line in lines)
    return "\n".join(new_lines)

 

 

Else post a screenshot of your table.


Have a great day!
Johannes
0 Kudos
MarcEsmeijer
New Contributor II

Dear Johannes,

thank you for your quick reply.

I have also tried it by creating a dictionary and then replacing the words mentioned in that dictionary as you have done. It works for most objects, however there is a problem. There are multiple polylines with the same code H13748 for example  (Can be seen under column called 'A1.1_Leggercode Origineel')  

At this moment the feature class is 'A1.1_Leggercode Origineel', therefore all polylines with the same value in the feature class (For example H13748) get the same replaced sentence because they share the same value  'A1.1_Leggercode Origineel'. However I wish for my code to loop through a different feature class, namely OBJECTID or preferably through the rows themselves. This would result in the code looping through and assigning different values to polylines with the same value in the feature class (A1.1_Leggercode origineel) 

MarcEsmeijer_1-1674655915936.png

 

I hope this provides some clarity in my question.

Thank you so much for your answer already.

Have a great day!

Kind regards,

Marc 

0 Kudos
JohannesLindner
MVP Frequent Contributor

I don't really understand, and it probably doesn't help that we're both not native speakers...

 

At this moment the feature class is 'A1.1_Leggercode Origineel', [...]

Looking at your screenshot, "Leggercode" and "Leggercode Origineel" are columns, not feature classes. Is that correct?

 

both H09916_A and H09916_B get the same replaced sentence because they share the same value  'A1.1_Leggercode Origineel'

No. They get the same replaced sentence, because they have the same value in the column "BAGGERBEHANDELING" (op de kant).

If you want to have different sentences for these features, you have to change one of the BAGGERBEHANDELING values first.

 

Maybe it would be easier to create the field Uitvoering in the Waterlopen table (the same table that has the BAGGERBEHANDELING column). Then you can do the calculation in there and then join the new field to Vooronderzoek.


Have a great day!
Johannes
0 Kudos
MarcEsmeijer
New Contributor II

Dear Johannes,

thanks again for your reply. 

Looking at your screenshot, "Leggercode" and "Leggercode Origineel" are columns, not feature classes. Is that correct?

Yes they are columns that is correct. I might not know the exact definition of a feature class then. It is impossible for me to replace the values from the column "BAGGERBEHANDELING".

Therefore I would like to know whether it is possible in ArcGIS to loop through the individual rows. So that it can generate unique values per row.

Have a great day!

Kind regards,

Marc

0 Kudos