Count of the different combination of two attributes in QGIS?

The name of the pictureThe name of the pictureThe name of the pictureClash 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.



enter image description here







share|improve this question




























    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.



    enter image description here







    share|improve this question
























      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.



      enter image description here







      share|improve this question














      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.



      enter image description here









      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 7 at 7:13









      Jochen Schwarze

      5,42021249




      5,42021249










      asked Aug 6 at 11:59









      Felipe_H81

      192




      192




















          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...)






          share|improve this answer



























            up vote
            3
            down vote













            You can use the tool "Statistics by categories" (in the processing toolbox in QGIS 3.2)



            enter image description here



            enter image description here



            enter image description here






            share|improve this answer




















            • It worked. Thanks Volta
              – Felipe_H81
              Aug 7 at 11:54

















            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.






            share|improve this answer



























              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:
              enter image description here






              share|improve this answer





























                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:



                enter image description here



                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):



                enter image description here



                Herein select your layer and the virtual field you just created, and under Count (distinct) you will find the count of your columns combinations:



                enter image description here



                The virtual field and the tool do dynamically refresh when data changes.






                share|improve this answer






















                  Your Answer







                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "79"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  convertImagesToLinks: false,
                  noModals: false,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );








                   

                  draft saved


                  draft discarded


















                  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






























                  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...)






                  share|improve this answer
























                    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...)






                    share|improve this answer






















                      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...)






                      share|improve this answer












                      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...)







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 6 at 13:31









                      snaileater

                      1,243512




                      1,243512






















                          up vote
                          3
                          down vote













                          You can use the tool "Statistics by categories" (in the processing toolbox in QGIS 3.2)



                          enter image description here



                          enter image description here



                          enter image description here






                          share|improve this answer




















                          • It worked. Thanks Volta
                            – Felipe_H81
                            Aug 7 at 11:54














                          up vote
                          3
                          down vote













                          You can use the tool "Statistics by categories" (in the processing toolbox in QGIS 3.2)



                          enter image description here



                          enter image description here



                          enter image description here






                          share|improve this answer




















                          • It worked. Thanks Volta
                            – Felipe_H81
                            Aug 7 at 11:54












                          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)



                          enter image description here



                          enter image description here



                          enter image description here






                          share|improve this answer












                          You can use the tool "Statistics by categories" (in the processing toolbox in QGIS 3.2)



                          enter image description here



                          enter image description here



                          enter image description here







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Aug 6 at 14:45









                          Volta

                          567211




                          567211











                          • 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




                          It worked. Thanks Volta
                          – Felipe_H81
                          Aug 7 at 11:54










                          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.






                          share|improve this answer
























                            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.






                            share|improve this answer






















                              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.






                              share|improve this answer












                              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.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Aug 6 at 12:47









                              Erik

                              1,38611




                              1,38611




















                                  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:
                                  enter image description here






                                  share|improve this answer


























                                    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:
                                    enter image description here






                                    share|improve this answer
























                                      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:
                                      enter image description here






                                      share|improve this answer














                                      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:
                                      enter image description here







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Aug 7 at 8:33

























                                      answered Aug 7 at 8:27









                                      BERA

                                      10.5k31435




                                      10.5k31435




















                                          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:



                                          enter image description here



                                          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):



                                          enter image description here



                                          Herein select your layer and the virtual field you just created, and under Count (distinct) you will find the count of your columns combinations:



                                          enter image description here



                                          The virtual field and the tool do dynamically refresh when data changes.






                                          share|improve this answer


























                                            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:



                                            enter image description here



                                            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):



                                            enter image description here



                                            Herein select your layer and the virtual field you just created, and under Count (distinct) you will find the count of your columns combinations:



                                            enter image description here



                                            The virtual field and the tool do dynamically refresh when data changes.






                                            share|improve this answer
























                                              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:



                                              enter image description here



                                              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):



                                              enter image description here



                                              Herein select your layer and the virtual field you just created, and under Count (distinct) you will find the count of your columns combinations:



                                              enter image description here



                                              The virtual field and the tool do dynamically refresh when data changes.






                                              share|improve this answer














                                              I would use the field calculator to create a virtual field containing the concatenation (i.e. combination) of your two fields, like this:



                                              enter image description here



                                              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):



                                              enter image description here



                                              Herein select your layer and the virtual field you just created, and under Count (distinct) you will find the count of your columns combinations:



                                              enter image description here



                                              The virtual field and the tool do dynamically refresh when data changes.







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Aug 8 at 7:52

























                                              answered Aug 7 at 7:03









                                              Jochen Schwarze

                                              5,42021249




                                              5,42021249






















                                                   

                                                  draft saved


                                                  draft discarded


























                                                   


                                                  draft saved


                                                  draft discarded














                                                  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













































































                                                  Comments

                                                  Popular posts from this blog

                                                  What does second last employer means? [closed]

                                                  Installing NextGIS Connect into QGIS 3?

                                                  One-line joke