Is there a way in QGIS or with pyqgis to populate a new column only when row and column names match (like in the example below)?

It's easy to do that with R but I can't find a solution in QGIS or with PyQGIS.

enter image description here


A solution without PyQGIS or Python

Use the following epression if(array_contains(array('test2', 'test3', 'test8'), "test"), attribute($currentfeature, "test"), NULL) in the "Expression Dialog"


Note: Beforehand you will need to create an array that will contain all the attributes' names, e.g. array('attribute1', 'attribute2', ...), i.e. array('Polygon1', 'Polygon2', 'Polygon3', 'Polygon4').

So, your final expression may be looking like if(array_contains(array('Polygon1', 'Polygon2', 'Polygon3', 'Polygon4'), "ID"), attribute($currentfeature, "ID"), NULL).

  • 1
    Thanks Taras, I have never used the array functions yet but I will have a look at the method you posted ! I'll keep you updated ! :)
    – wanderzen
    Aug 20 '20 at 13:04
  • I don't understand how I should create an array of all my attributes names first... In the example you provided, I don't see where you did that ? Could you possibly add some details to your anwser please ? Many thanks in advance ! :)
    – wanderzen
    Aug 22 '20 at 8:14
  • 1
    It was done here array('test2', 'test3', 'test8'), so you simply do array('Polygon1', 'Polygon2', 'Polygon3', 'Polygon4')
    – Taras
    Aug 22 '20 at 8:16
  • Aaaah I thought that I had to create the array somewhere first before using the expression ! Sorry for the silly question and thank you so much for your help Taras !
    – wanderzen
    Aug 22 '20 at 8:21
  • There are no silly questions! Always welcome! And instead of "test" use yours "ID".
    – Taras
    Aug 22 '20 at 8:39

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .