4

I'm trying to replace the values of the column A and B by the value of the row 1 and row 2 of column C. (15B70C should be replaced by 63F200 and 94JH5 by 35V145)

How can I automate that in the simplest way possible?

enter image description here

Linda
  • 341
  • 1
  • 12

2 Answers2

6

If I understood correctly, the following script would work for you. First, select the layer and use the script in QGIS Python Editor.

# get layer
layer = iface.activeLayer()
# convert features to list
features = list(layer.getFeatures())
# sort features to be sure it is sorted by id. it is crucial
features.sort(key=lambda f: f["id"])

dpr = layer.dataProvider()

get fields' index

A = dpr.fieldNameIndex("A") B = dpr.fieldNameIndex("B")

for i in range(0, len(features), 2): # make changes dpr.changeAttributeValues({features[i].id(): {A: features[i]["C"], B: features[i+1]["C"]}})

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
  • Thanks for you reply Kadir !! Your script is almost what I'm trying to do ! I've added a new picture to clarify my question (sorry if my explanations were fuzzy !) – Linda Feb 26 '21 at 07:19
  • I guess I just have to change the last line of your code by features[i].id(): {B: features[i+1]["C"]}}) right ? :) – Linda Feb 26 '21 at 07:28
  • With the edit you made, the script raise a SyntaxError:

    File "<string>", line 24 B: features[i+1]["C"]}) SyntaxError: invalid syntax

    – Linda Feb 26 '21 at 08:26
0

Modify the columns using the field calculator by simply putting your desired value in single paranthesis, eg. '35V145'. Now every field in column A reads "35V145".

Erik
  • 16,269
  • 1
  • 24
  • 43
  • The problem is that I have to do that for more than 400 hundred id :( ... I would like to find a solution that would do the trick automatically (if it is possible) – Linda Feb 25 '21 at 15:38
  • Then you should describe your issue completely, I guess. – Erik Feb 26 '21 at 07:36