1

I want to replace a text from one collumn into another text in a different collumn and base the condition on two different input collumn/fields.

Collumn 1: Integers (from 1 to 10)

Collumn 2: 100 different combinations of this text type - '1 and 10'

New collumn:

When appears '10' on collumn1 and '1 and 10' on collumn2 replace with 'YPC5' AND ( other example) when appears '1' substitute by 'YPC5' and when appears '10' substitute for 'YPC8', becoming: 'YPC5 and YPC8'. But, if the field1 changes for '9' then 'YPC5 and YPC7'.

enter image description here

Is there a python script for doing this in a easier way than use a gigant 'replace(' and 'case then else' combined expression?

enter image description here

  • 1
    Your question (which you posted a duplicate of today) is difficult to understand. For example you say "Collumn 1: 100 different combinations of this text type - '1 and 10'" then show a screenshot of column 1 where no cell has the value '1 and 10'. Try to edit this question instead to make it understandable and delete your duplicate post from today. – BERA Sep 20 '18 at 14:43
  • is shapefile column 1, not matrix column 1. look at the matrix: when shapefile column 1 is equal 10, '1' will be replaced by 'YC1' and '10' by 'YC11'. So, for a shapefile column 2 with this string " 1 plus 10" i want to have this "YC1 plus YC11", and this replacement to be made ona anew field (called it shapefile column 3). – Rui Sep 20 '18 at 14:48
  • Ok. So when you say "When appears '10' on collumn1 and '1 and 10' on collumn2" you swapped the columns and actually mean: "When appears '10' on collumn2 and '1 and 10' on collumn1" . You describe the columns as: Collumn 1: 100 different combinations of this text type - '1 and 10' Collumn 2: Integers (from 1 to 10) – BERA Sep 20 '18 at 14:59
  • yes, can you help me now? :) – Rui Sep 20 '18 at 15:03
  • The only reason you are not getting the answer you are looking for is that you post unclear questions. If it was clear you would get an answer from someone within minutes. What is the third screenshot for? There is no YPC5 etc. in it. – BERA Sep 20 '18 at 15:19
2

When having a situation like this one, you need to take a look to dictionaries.

your_dict = {'1':'YPC5', '2', 'YPC6', '3':'YPC7', ... , old_value: new_value}

then when you want to substitute you just need to do:

new_value = your_dict[old_value]

in your case, you need to go for the value of each cell and do:

new_cell = '{} plus {}'.format(your_dict[column], your_dict[row])
  • Could you just build a bit of the script since i'm newbie in python, just started learning now.. This is the matrix with replacements to be made: i.stack.imgur.com/sBfa9.png – Rui Sep 20 '18 at 14:19
  • In the second matrix it was a pure substitution, "[value of row substituted] plus [value of col substituted]". Now it's different. I can think of nested dictionaries, where from each key of the first dictionary (dict for rows) contains a second dictionary with all the possible values for the columns. – ImanolUr Sep 21 '18 at 10:41
  • Can we chat please? – Rui Sep 27 '18 at 8:37

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.