TransWikia.com

Split the column into several and unpivot the columns

Geographic Information Systems Asked on July 28, 2021

I currently have 2 columns NOM_CONCAT and Number of concat(COUNT_RSF_)

which is composed of" concat "from 0 to 5

A1, B1, C1 ...

A2

A3, B3

.
.

My goal and to separate the column NOM_CONCAT in several columns under PyQGIS
except that this does not mean this to separate.

Without hiding, the end goal is to rotate the generated columns.

from PyQt5.QtCore import *
from qgis.core import *
from qgis.PyQt.QtCore import QVariant
uri="C:impression_planminiRSFFicheCanalisation.shp"

v1= QgsVectorLayer(uri, "RPG_grille_2018", "ogr")
all_features = v1.getFeatures()
v1.startEditing()
pr = v1.dataProvider()
#l=1
idx = v1.fields().lookupField('COUNT_RSF_')
idmax =  v1.maximumValue(idx)

print(idmax)
v1 = iface.activeLayer()
fieldindex = v1.fields().indexFromName("NOM_CONCAT") 

v1.startEditing() 
for feat in v1.getFeatures(): 

    if feat[fieldindex]: 
        fields = feat[fieldindex].split(', ')
        print(fields)

        for i in range(1, len(fields)): 

            feat[fieldindex + i] = fields[i - 1] 
            v1.updateFeature(feat) 
    else: 
        continue 
v1.commitChanges()

QgsProject.instance().addMapLayer(v1)
v1.updateFields

enter image description here

One Answer

You can use "Split features by character" tool.

uri = r"C:impression_planminiRSFFicheCanalisation.shp"

layer = QgsVectorLayer(uri, "", "ogr")

new_layer = processing.runAndLoadResults(
                "native:splitfeaturesbycharacter",
                {'INPUT': layer, 
                 'FIELD':'NOM_CONCAT',
                 'CHAR':', ',
                 'REGEX': False,
                 'OUTPUT':'TEMPORARY_OUTPUT'})["OUTPUT"]

Before:

enter image description here

After:

enter image description here

Correct answer by Kadir Şahbaz on July 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP