There are "Code"
and "Color"
fields in my layer like this: (Sample 6 rows)
Code | Color |
---|---|
A | Red |
B | Blue |
A | Red |
A | Red |
B | Blue |
C | Green |
All A
s are Red
, all B
s are Blue
and so on.
I can get the unique values for each field:
layer = iface.activeLayer()
code_index = layer.fields().indexFromName("Code")
code_unique = layer.uniqueValues(code_index)
color_index = layer.fields().indexFromName("Color")
color_unique = layer.uniqueValues(color_index)
print(code_unique)
print(color_unique)
# OUTPUT
# {'A', 'C', 'B'}
# {'Blue', 'Red', 'Green'}
As you can see, the order of two outputs doesn't match. (When you try, you might get an ordered result, but it depends and is not reliable)
How can I get unique value pairs like [('A', 'Red'), ('B', 'Blue'), ('C', 'Green')]
using PyQGIS?
I couldn't find groupby
-like method. It can be done by means of groupby(["Code", "Color"]).first()
in GeoPandas.