Count of the different combination of two attributes in QGIS?
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
I've been trying in vain to use the field calculator in QGIS 2.8 to count the combinations of two attributes in two separate columns.
First column is called "Alds_klass"
and second is called "bygn_klass"
. I want to count the different combinations of the two columns, not the sum, but the number of each combination.
qgis field-calculator count cross-tabulation
add a comment |Â
up vote
3
down vote
favorite
I've been trying in vain to use the field calculator in QGIS 2.8 to count the combinations of two attributes in two separate columns.
First column is called "Alds_klass"
and second is called "bygn_klass"
. I want to count the different combinations of the two columns, not the sum, but the number of each combination.
qgis field-calculator count cross-tabulation
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I've been trying in vain to use the field calculator in QGIS 2.8 to count the combinations of two attributes in two separate columns.
First column is called "Alds_klass"
and second is called "bygn_klass"
. I want to count the different combinations of the two columns, not the sum, but the number of each combination.
qgis field-calculator count cross-tabulation
I've been trying in vain to use the field calculator in QGIS 2.8 to count the combinations of two attributes in two separate columns.
First column is called "Alds_klass"
and second is called "bygn_klass"
. I want to count the different combinations of the two columns, not the sum, but the number of each combination.
qgis field-calculator count cross-tabulation
edited Aug 7 at 7:13
Jochen Schwarze
5,42021249
5,42021249
asked Aug 6 at 11:59
Felipe_H81
192
192
add a comment |Â
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
6
down vote
If you're ready NOT to use the field calculator i would create a virtual layer based on the following query (to adapt) :
select count(*) as tot, col_a, col_b from my_table
group by col_a, col_b
order by tot desc
Which would be "dynamic" ...
(implies to use a newer QGIS version...)
add a comment |Â
up vote
3
down vote
You can use the tool "Statistics by categories" (in the processing toolbox in QGIS 3.2)
It worked. Thanks Volta
â Felipe_H81
Aug 7 at 11:54
add a comment |Â
up vote
0
down vote
Create a new column and fill it with a case when
or if
clause looping over all possible values. Assign each combination a different letter of the alphabet, and then do a statistical analysis of the new column.
Or just load it into a database.
add a comment |Â
up vote
0
down vote
If you want to populate Count_Al_b
with the total count of combinations Alds_class+bygn_class
you can use collections.Counter:
...where elements are stored as dictionary keys and their counts are
stored as dictionary values.
print 'start'
from qgis.core import *
import qgis.utils
from qgis.core import QgsVectorLayer
from collections import Counter
alds = "Alds_klass"
bygn = "bygn_klass"
totcount = "Count_Al_b"
datafile = '/media/bera/my_03.shp' #Change
layer = QgsVectorLayer(datafile, 'temp', 'ogr')
c = Counter([(f[alds],f[bygn]) for f in layer.getFeatures()])
layer.startEditing()
it = layer.getFeatures()
for feat in it:
layer.changeAttributeValue(feat.id(),layer.fieldNameIndex(totcount),c[(feat[alds],feat[bygn])])
#layer.fields().indexFromName(totcount) python 3/QGIS 3
layer.commitChanges()
print 'stop'
Example:
add a comment |Â
up vote
0
down vote
I would use the field calculator to create a virtual field containing the concatenation (i.e. combination) of your two fields, like this:
The underscore '_'
ist just for better readabilty and does not affect the result.
Then use the 'Show statistical summary' button (the button with the big sigma):
Herein select your layer and the virtual field you just created, and under Count (distinct) you will find the count of your columns combinations:
The virtual field and the tool do dynamically refresh when data changes.
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
If you're ready NOT to use the field calculator i would create a virtual layer based on the following query (to adapt) :
select count(*) as tot, col_a, col_b from my_table
group by col_a, col_b
order by tot desc
Which would be "dynamic" ...
(implies to use a newer QGIS version...)
add a comment |Â
up vote
6
down vote
If you're ready NOT to use the field calculator i would create a virtual layer based on the following query (to adapt) :
select count(*) as tot, col_a, col_b from my_table
group by col_a, col_b
order by tot desc
Which would be "dynamic" ...
(implies to use a newer QGIS version...)
add a comment |Â
up vote
6
down vote
up vote
6
down vote
If you're ready NOT to use the field calculator i would create a virtual layer based on the following query (to adapt) :
select count(*) as tot, col_a, col_b from my_table
group by col_a, col_b
order by tot desc
Which would be "dynamic" ...
(implies to use a newer QGIS version...)
If you're ready NOT to use the field calculator i would create a virtual layer based on the following query (to adapt) :
select count(*) as tot, col_a, col_b from my_table
group by col_a, col_b
order by tot desc
Which would be "dynamic" ...
(implies to use a newer QGIS version...)
answered Aug 6 at 13:31
snaileater
1,243512
1,243512
add a comment |Â
add a comment |Â
up vote
3
down vote
You can use the tool "Statistics by categories" (in the processing toolbox in QGIS 3.2)
It worked. Thanks Volta
â Felipe_H81
Aug 7 at 11:54
add a comment |Â
up vote
3
down vote
You can use the tool "Statistics by categories" (in the processing toolbox in QGIS 3.2)
It worked. Thanks Volta
â Felipe_H81
Aug 7 at 11:54
add a comment |Â
up vote
3
down vote
up vote
3
down vote
You can use the tool "Statistics by categories" (in the processing toolbox in QGIS 3.2)
You can use the tool "Statistics by categories" (in the processing toolbox in QGIS 3.2)
answered Aug 6 at 14:45
Volta
567211
567211
It worked. Thanks Volta
â Felipe_H81
Aug 7 at 11:54
add a comment |Â
It worked. Thanks Volta
â Felipe_H81
Aug 7 at 11:54
It worked. Thanks Volta
â Felipe_H81
Aug 7 at 11:54
It worked. Thanks Volta
â Felipe_H81
Aug 7 at 11:54
add a comment |Â
up vote
0
down vote
Create a new column and fill it with a case when
or if
clause looping over all possible values. Assign each combination a different letter of the alphabet, and then do a statistical analysis of the new column.
Or just load it into a database.
add a comment |Â
up vote
0
down vote
Create a new column and fill it with a case when
or if
clause looping over all possible values. Assign each combination a different letter of the alphabet, and then do a statistical analysis of the new column.
Or just load it into a database.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Create a new column and fill it with a case when
or if
clause looping over all possible values. Assign each combination a different letter of the alphabet, and then do a statistical analysis of the new column.
Or just load it into a database.
Create a new column and fill it with a case when
or if
clause looping over all possible values. Assign each combination a different letter of the alphabet, and then do a statistical analysis of the new column.
Or just load it into a database.
answered Aug 6 at 12:47
Erik
1,38611
1,38611
add a comment |Â
add a comment |Â
up vote
0
down vote
If you want to populate Count_Al_b
with the total count of combinations Alds_class+bygn_class
you can use collections.Counter:
...where elements are stored as dictionary keys and their counts are
stored as dictionary values.
print 'start'
from qgis.core import *
import qgis.utils
from qgis.core import QgsVectorLayer
from collections import Counter
alds = "Alds_klass"
bygn = "bygn_klass"
totcount = "Count_Al_b"
datafile = '/media/bera/my_03.shp' #Change
layer = QgsVectorLayer(datafile, 'temp', 'ogr')
c = Counter([(f[alds],f[bygn]) for f in layer.getFeatures()])
layer.startEditing()
it = layer.getFeatures()
for feat in it:
layer.changeAttributeValue(feat.id(),layer.fieldNameIndex(totcount),c[(feat[alds],feat[bygn])])
#layer.fields().indexFromName(totcount) python 3/QGIS 3
layer.commitChanges()
print 'stop'
Example:
add a comment |Â
up vote
0
down vote
If you want to populate Count_Al_b
with the total count of combinations Alds_class+bygn_class
you can use collections.Counter:
...where elements are stored as dictionary keys and their counts are
stored as dictionary values.
print 'start'
from qgis.core import *
import qgis.utils
from qgis.core import QgsVectorLayer
from collections import Counter
alds = "Alds_klass"
bygn = "bygn_klass"
totcount = "Count_Al_b"
datafile = '/media/bera/my_03.shp' #Change
layer = QgsVectorLayer(datafile, 'temp', 'ogr')
c = Counter([(f[alds],f[bygn]) for f in layer.getFeatures()])
layer.startEditing()
it = layer.getFeatures()
for feat in it:
layer.changeAttributeValue(feat.id(),layer.fieldNameIndex(totcount),c[(feat[alds],feat[bygn])])
#layer.fields().indexFromName(totcount) python 3/QGIS 3
layer.commitChanges()
print 'stop'
Example:
add a comment |Â
up vote
0
down vote
up vote
0
down vote
If you want to populate Count_Al_b
with the total count of combinations Alds_class+bygn_class
you can use collections.Counter:
...where elements are stored as dictionary keys and their counts are
stored as dictionary values.
print 'start'
from qgis.core import *
import qgis.utils
from qgis.core import QgsVectorLayer
from collections import Counter
alds = "Alds_klass"
bygn = "bygn_klass"
totcount = "Count_Al_b"
datafile = '/media/bera/my_03.shp' #Change
layer = QgsVectorLayer(datafile, 'temp', 'ogr')
c = Counter([(f[alds],f[bygn]) for f in layer.getFeatures()])
layer.startEditing()
it = layer.getFeatures()
for feat in it:
layer.changeAttributeValue(feat.id(),layer.fieldNameIndex(totcount),c[(feat[alds],feat[bygn])])
#layer.fields().indexFromName(totcount) python 3/QGIS 3
layer.commitChanges()
print 'stop'
Example:
If you want to populate Count_Al_b
with the total count of combinations Alds_class+bygn_class
you can use collections.Counter:
...where elements are stored as dictionary keys and their counts are
stored as dictionary values.
print 'start'
from qgis.core import *
import qgis.utils
from qgis.core import QgsVectorLayer
from collections import Counter
alds = "Alds_klass"
bygn = "bygn_klass"
totcount = "Count_Al_b"
datafile = '/media/bera/my_03.shp' #Change
layer = QgsVectorLayer(datafile, 'temp', 'ogr')
c = Counter([(f[alds],f[bygn]) for f in layer.getFeatures()])
layer.startEditing()
it = layer.getFeatures()
for feat in it:
layer.changeAttributeValue(feat.id(),layer.fieldNameIndex(totcount),c[(feat[alds],feat[bygn])])
#layer.fields().indexFromName(totcount) python 3/QGIS 3
layer.commitChanges()
print 'stop'
Example:
edited Aug 7 at 8:33
answered Aug 7 at 8:27
BERA
10.5k31435
10.5k31435
add a comment |Â
add a comment |Â
up vote
0
down vote
I would use the field calculator to create a virtual field containing the concatenation (i.e. combination) of your two fields, like this:
The underscore '_'
ist just for better readabilty and does not affect the result.
Then use the 'Show statistical summary' button (the button with the big sigma):
Herein select your layer and the virtual field you just created, and under Count (distinct) you will find the count of your columns combinations:
The virtual field and the tool do dynamically refresh when data changes.
add a comment |Â
up vote
0
down vote
I would use the field calculator to create a virtual field containing the concatenation (i.e. combination) of your two fields, like this:
The underscore '_'
ist just for better readabilty and does not affect the result.
Then use the 'Show statistical summary' button (the button with the big sigma):
Herein select your layer and the virtual field you just created, and under Count (distinct) you will find the count of your columns combinations:
The virtual field and the tool do dynamically refresh when data changes.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I would use the field calculator to create a virtual field containing the concatenation (i.e. combination) of your two fields, like this:
The underscore '_'
ist just for better readabilty and does not affect the result.
Then use the 'Show statistical summary' button (the button with the big sigma):
Herein select your layer and the virtual field you just created, and under Count (distinct) you will find the count of your columns combinations:
The virtual field and the tool do dynamically refresh when data changes.
I would use the field calculator to create a virtual field containing the concatenation (i.e. combination) of your two fields, like this:
The underscore '_'
ist just for better readabilty and does not affect the result.
Then use the 'Show statistical summary' button (the button with the big sigma):
Herein select your layer and the virtual field you just created, and under Count (distinct) you will find the count of your columns combinations:
The virtual field and the tool do dynamically refresh when data changes.
edited Aug 8 at 7:52
answered Aug 7 at 7:03
Jochen Schwarze
5,42021249
5,42021249
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f291846%2fcount-of-the-different-combination-of-two-attributes-in-qgis%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password