Attribute analysis and statistics
How to count villages in each district using a shapefile
Load a village shapefile, group its records by the district field and count them with the Aggregate tool, then inspect and export the summary table.
You will learn
- Open a zipped shapefile in Orbis.
- Find the field that holds the category you want to count.
- Group records by a field and count them with Aggregate (group and summarize).
- Read, sort and export the summary table as CSV.
Data for this tutorial
Sample villages (synthetic) Download villages.zip
- Format
- ESRI Shapefile (ZIP: .shp, .shx, .dbf, .prj, .cpg)
- Geometry
- Point
- CRS
- EPSG:4326 (WGS 84 longitude, latitude)
- Content
- 25 villages in 5 districts of 2 states
Fields used in the tutorials
| Field | Type | Description |
|---|---|---|
village_id | text | Identifier V001 to V025. |
village | text | Village name. Common names repeat (Rampur, Shivpur, Madhopur), as they do in real gazetteers. |
district | text | District: Rampur, Varanasi, Jaunpur, Patna or Gaya. |
state | text | State: Uttar Pradesh or Bihar. |
population | integer | Residents. One village (Amarpur) has no recorded population (NULL). |
households | integer | Households. |
has_school | text | yes or no. |
Licence: CC0 1.0. All tutorial data is synthetic and generated by Orbis. About the datasets
The task
You have a point shapefile of villages. Every village has a district attribute, and you want a table that says how many villages each district has. In a spreadsheet you would build a pivot table; in GIS the operation is called grouping (or aggregation): records with the same value in a field form a group, and a function such as COUNT is computed for every group.
You will use the Sample villages dataset: 25 villages in five districts. Download villages.zip from the box above; you do not need to unzip it.
Steps
Load the shapefile
Open Orbis and drag
villages.zipfrom your file manager onto the map. You can also use Layer ▸ Add data from files… (Ctrl+O) and choose the file.Orbis reads the
.shp,.shx,.dbf,.prjand.cpgfiles inside the ZIP together. A layer called villages appears in the Layers panel and 25 points appear on the map, in Uttar Pradesh and Bihar.
The villages layer after loading villages.zip.
Find the district field
- Click villages in the Layers panel to make it the active layer, then press F6 (or right-click the layer and choose Open attribute table). The attribute table opens at the bottom of the window.
Look at the column headers. The
districtcolumn holds the name of the district of each village; that is the field you will group by. Right-click thedistrictheader and choose Field statistics & unique values if you want to see the five district names before you start.
The attribute table: one row per village, with its district.
Group and count
In the attribute table toolbar, click Group and summarize (the grouping icon just before the download icon, Export…). The Aggregate (group and summarize) tool opens in the right panel with villages already chosen as Input layer or table.
You can open the same tool from Analysis ▸ Group and summarize (aggregate)… or by searching for "aggregate" in the Processing Toolbox.
- Under Group by fields, tick district.
Under Aggregates, the tool already contains one aggregate, Count records, which is COUNT(*): the number of records in each group. Type
village_countin its output field name box (the box that showscountin grey) so that the column has a clear name.
Group by district, count the records, and name the result village_count.
- Click Run. The tool runs in a background worker; for 25 records it finishes at once.
Inspect the result
- A new table layer, Summary by district (villages), is added to the Layers panel and opens in the attribute table. Click the
village_countheader to sort by the count; click again to sort in descending order.
Expected result
The summary table has one row per district and two columns:
| district | village_count |
|---|---|
| Gaya | 4 |
| Jaunpur | 8 |
| Patna | 6 |
| Rampur | 5 |
| Varanasi | 2 |
The counts add up to 25, the number of villages in the layer. Rows are sorted by the grouping field (alphabetically for text).

Export the summary
- With Summary by district (villages) shown in the attribute table, click Export… in the table toolbar.
- Choose CSV (UTF-8) as the format, check the file name and click Export. Orbis writes the file, reads it back to check it, and your browser downloads it. The CSV opens in any spreadsheet program.
Common mistakes
- Grouping by the wrong field. Grouping by
villagecounts how often each village name occurs, not villages per district. That is a different, also useful question: see How to count repeated attribute values. - Using Count values instead of Count records. Count values (non-NULL) counts only records where the chosen field is not empty. Use Count records when you want every village, whatever its other fields contain.
- Spelling variants. Groups are formed from exact values. "Jaunpur", "jaunpur" and "Jaunpur " (with a trailing space) are three different groups. Clean the values first (see the troubleshooting section).
- Selected features only. If Selected features only is ticked under the input, only the selected villages are counted.
Troubleshooting
- The tool says "Add at least one aggregate". You removed the Count aggregate. Click Count next to Add under Aggregates.
- More districts than expected. Open Field statistics & unique values for
districtin the attribute table. Values that differ only in case or spaces are separate groups. Correct them while editing (Ctrl+E) or with the Field calculator (for exampletrim(lower("district"))into a new field), and group by the corrected field. - A district shows a count you did not expect. Check whether a filter is set under Only records where; records for which the filter is not true are left out before counting.
- The ZIP does not load. A shapefile ZIP must contain at least the
.shp,.shxand.dbffiles with the same base name. Orbis tells you which files are missing.