How to Join CSV Data to a Shapefile in QGIS: Attribute Joins Step by Step
- Primary Signal: How to Join CSV Data to a Shapefile in QGIS: Attribute Joins Step by Step
- Overview: A shapefile knows where things are, but a spreadsheet knows what they are. Joining a CSV to a shapefile on a shared key brings the two together, and the whole trick is making sure ...
- Verification: Analyzed and compiled by FreeSky GeoData editorial monitoring desk.
Most GIS projects reach a point where the map has the geography but not the information. You have a shapefile of districts, and a spreadsheet of population figures. You have a layer of schools, and a CSV of enrolment numbers. The geometry is in one place and the data is in another, and the job is to bring them together. In QGIS that is called an attribute join, and it works by matching a shared key field: a district code, a school ID, or any column that appears in both the layer and the table, with identical values on each side.
Joins are one of the most common operations in GIS, and one of the most common sources of frustration, because they fail silently. If the key values do not match exactly, QGIS does not warn you; it simply leaves the joined fields blank for the rows that did not match. A single trailing space, a leading zero dropped by a spreadsheet, or a text field joined to a number field is enough to break the match. This guide covers the setup that prevents those failures.
Prepare the CSV before you join anything
Open the CSV in a spreadsheet application first and inspect the key column closely. The most frequent problem is data type mismatch: the shapefile stores a district code as text, such as 0123, while the spreadsheet stores it as a number, 123, and the leading zero has vanished. Decide which side is correct, then make the other side match. In the spreadsheet, format the column as text before typing or pasting values, and watch out for invisible trailing spaces, which you can remove with a trim function.
Also check for duplicate keys. A join expects each key in the CSV to match at most one row in the layer, or at least to be consistent. If the same district code appears twice in the CSV with different population figures, QGIS will join the first match and ignore the second, which is rarely what you want. Clean duplicates in the spreadsheet before loading it. Save the file as CSV with UTF-8 encoding, and keep the header row simple: short names, no spaces, no special characters.
Run the join in QGIS
Load the CSV into QGIS with Layer, Add Layer, Add Delimited Text Layer, making sure QGIS detects the correct delimiter and that the key column is read as the right type. Then open the shapefile's Layer Properties, go to the Joins tab, and click the plus button to add a join. Select the CSV as the join layer, pick the key field in the CSV and the matching key field in the shapefile, and choose which CSV fields to bring across. Untick any columns you do not need, because joining twenty columns when you need three only slows the project down.
Click OK and open the shapefile's attribute table. The CSV columns should now appear at the end, filled in for every row that matched. Scan the table for blanks: a blank in a joined field means that row's key did not match anything in the CSV. If many rows are blank, the keys do not match, and the fix is in the data, not in the join dialog. Common culprits are case differences, extra spaces, or one side storing the key as text and the other as a number. Fix the source data and re-run the join.
Make the join permanent and use it
A join in QGIS is temporary by default: it lives in the project file, not in the shapefile, and it disappears if you share the shapefile without the CSV. To make it permanent, right-click the layer, choose Export, Save Features As, and save a new file. The joined fields are written into the new file as ordinary attributes. Do this only after you have checked the join is correct, because an exported file bakes in any mismatches.
Once the data is joined, it becomes the basis for everything else: a choropleth map coloured by the joined population figures, labels showing the joined values, or a field calculator expression that divides one joined column by another. A join also combines with selections and filters, so you can map only the districts where the joined value passes a threshold. That is the real payoff: the moment a spreadsheet becomes a map, the patterns in the numbers become visible in a way a table never shows.
Frequently Asked Questions
Q: Why are my joined fields blank after a join in QGIS?
A: The key values do not match exactly. Check for trailing spaces, case differences, dropped leading zeros, and text-versus-number type mismatches between the two key columns.
Q: Can I join a CSV to a shapefile if there is no common field?
A: Not with an attribute join, which needs a shared key. If there is no key, you may need a spatial join instead, which matches features by location rather than by an attribute value.
Q: Does a QGIS join change my original shapefile?
A: No. A join is stored in the project file and does not modify the source data. Export the layer to a new file if you want the joined fields saved permanently.
📖 Continue Reading the Full Story
Continue reading this article on FreeSky GeoData.
⚡ Access Full Content Now