-2

Is there a way I can use pyqgis to count the number of features in different categories in one layer? I am using rule based renderer, and realizing that for some rules the feature count is actually empty zero, so I would like to remove those categories:

enter image description here

closed as off-topic by PolyGeo Jan 21 at 8:42

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

3

Use getFeatures to list all records and count with collections.Counter:

from collections import Counter
layer = iface.activeLayer()
field = 'KKOD' #Change to match your fieldname
c = Counter([f[field] for f in layer.getFeatures()])

>>c
Counter({3: 6875, 7: 2853, 5: 1642, 6: 1580, 13: 1212, 8: 326, 4: 171, 11: 61, 12: 21, 1: 20, 2: 11})

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.