Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I read in a CSV file for linking to a .shp file. All the variables that are just numbers came in as text. How do I change the text variables to numeric in QGIS?

share|improve this question

4 Answers 4

up vote 16 down vote accepted

I once had the same question. The answer is that you put a file in the same folder with the ending csvt. For example your file name is xyzdata.csv you add the file xyzdata.csvt

This one you can edit with editor for example. And in it you set the data type like this. "Integer","String","Integer","Integer","String","Real"

Integer is now the variable for the first column, string for the second and so on... Make sure the files are named identically.

Edit: See here too if needed: http://underdark.wordpress.com/2011/03/07/how-to-specify-data-types-of-csv-columns-for-use-in-qgis/

share|improve this answer
    
Thanks a lot1 so is it true that all CSV files will be read in as text? Maybe I imagined this but when I was getting started with QGIS a little while ago I linked a CSV file and numbers were numbers. Seems that this info should be in the users manual. (maybe I missed it) –  HealthMaps Jul 16 '12 at 19:11
    
Yea, that was pretty weird for me too. But if you have only a few columns, this method is easy to perform. So...did it actually work out for you? –  Shepherdjo Jul 17 '12 at 10:41
    
Yes, it works perfectly! Watch out setting up your CSVT file in Excel; extra quotation marks get stuck in etc. I use SAS or R to make the files I want to link so it is not too much trouble to make a CSVT file along with the CSV file. Seems this needs to be fixed. Another GIS I use see numbers in a CSV column, it reads them in as numbers. –  HealthMaps Jul 17 '12 at 16:09
    
I found a plugin MMQGIS that will change a column from text to float or float to text. Works only for a shape file, not a linked file or an imported csv file. See michaelminn.com/linux/mmqgis –  HealthMaps Jul 17 '12 at 17:40
    
Good to hear that. And thx for the plug-in hint. I would be grateful too, if you accept my answer then, will you? –  Shepherdjo Jul 18 '12 at 13:36

MMQGis caused this problem, and, as it turns out, it can fix it to.

Once you've joined your Shapefile & CSV Data, use MMQGis's Modify > Text to Float tool.

As usual, it leaves a trail of junk intermediate shapefiles but what else is new

share|improve this answer

Another alternative in case of large datasets to be managed and obviously time consuming to to proceed with the field calculator (without looping) or within a text editor : - save your data as CSV + XY + variable for the joint with our polugone shapefile - upload your CSV with the plug in (import csv) then save as shapefile (point) - then close q gis - back to the folder with the shapefile point, duplicate the corresponding dbf table / rename - open Qgis - upload the new dbf and join to shapefile (polygone features)

One stone, two pigeons : you have potentally 1) a shapefile point with all the data and all the possibility of represention 2) and independant shapefile with dbf table joined (editable !) linked to polygone for thematic cartography

This is a bit rude , but quite straightforward ...

bs_epidemio

share|improve this answer
2  
While this answer discusses how to get the .csv file into an editable format, it does not address the original question of how to convert an attribute value from a text to a number. While it is important to have quality, focused questions, it is equally critical that the answers to those questions be as focused and relevant as possible to what was asked originally. –  Get Spatial Sep 23 '12 at 0:37

Rather than messing with the input file, trying to get everything picture perfect, just use the field calculator to convert the text to a number / real. See the examples below:

Field Calculator

Field Calculator to Number

Field Calculator to Real

Attribute Table After Function

share|improve this answer
    
When I brought the CSV file in I could not edit it. The edit button was greyed out. How can I get it to be editable? –  HealthMaps Jul 16 '12 at 20:21
    
I'm not sure you can edit a csv through qGIS. I think I misread your question in that you had already imported the csv data across to the shapefile. –  Geoist Jul 16 '12 at 23:42
    
I did the link, .shp to a csv file which had number columns but were really text. Apparently one cannot do any editing either on a CSV file on its own or if it is linked to a SHP file. SO I saved the linked file and then reloaded it and was able to edit the text number files to an integer format as you suggested. Seems using the .csvt file works best if you have a lot of text columns that need to be converted and then use the field calculator method if there are not so many. It would be useful to be able to edit the CSV file on its own, not linked to anything. –  HealthMaps Jul 17 '12 at 1:55
    
Either way, as suggested in the answers here, will work. It just depends on your unique circumstances which one you decide on. –  Geoist Jul 18 '12 at 0:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.