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?
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"]}})
features[i].id(): {B: features[i+1]["C"]}}) right ? :)
– Linda
Feb 26 '21 at 07:28
File "<string>", line 24 B: features[i+1]["C"]}) SyntaxError: invalid syntax
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".
A1, or three? – Kadir Şahbaz Feb 25 '21 at 16:11Ashould be replaced byC, but in which row? The first one (35V145) or the second one (63F200)? Is it important? – Kadir Şahbaz Feb 25 '21 at 17:24