Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My goal is to take a CSV that contains 50 rows and one column and import the data into a feature class with over 3,000 rows using arcpy. I want to pick a random row from the CSV and then iterate through the rows in the featureclass.

I will need to use this workflow constantly, but am unsure of how to read the CSV into arcpy.

share|improve this question
So you want to random select a row in the CSV and insert the same value into all 3000 rows? – ianbroad Apr 3 '14 at 16:39
I want to random select a row, insert that value into the feature class row, and then repeat. So, I don't want the same value in all 3000 – BrianJBaldwin Apr 3 '14 at 16:42
Is the .csv absolutely necessary? Why not just insert a randomly generated value into the FC using only Python? – Aaron Apr 3 '14 at 16:45
I was able to do that for fake phone number generation, but it gets tricky with names. I was thinking the best way would be to have a list of fake names in a csv, rather than trying to generate names. – BrianJBaldwin Apr 3 '14 at 16:46
OK, I see now. Thanks! Please consider updating your post to include this ancillary info. – Aaron Apr 3 '14 at 16:49
up vote 3 down vote accepted

I would use random.choice() to make a random selection from the .csv file and insert that into your FC rows using an Update Cursor.

import arcpy, csv, random

# Read csv file with names
file = r'C:\path\to\your\file.csv'
reader = csv.reader(open(file, "rb"), delimiter = ",", skipinitialspace=True)
listofnames = [name for line in reader for name in line]

# Update rows with random name selection
fc = r'C:\path\to\your\fc'

with arcpy.da.UpdateCursor(fc, ["yourfield"]) as cursor:
    for row in cursor:
        rand = random.choice(listofnames)
        row[0] = rand
share|improve this answer
Aaron, thanks a great deal. Just for my own clarification, when is the syntax for listofnames doing? Your creating a list, and the 'name for line in reader' seems to make sense, but why does it need to be repeated? – BrianJBaldwin Apr 3 '14 at 17:58
This is a list comprehension that generates a list from your csv file. It is looping through the lines because my csv values were in a vertical structure as opposed to a horizontal structure. Essentially, it creates a list like ['a','b','c'] that random.choice() can select from rather than one that it cannot e.g. [['a','b','c']]. – Aaron Apr 3 '14 at 18:16
import arcpy
import random
import csv

fc = "C:/stuff.shp"

csv_rows = []

with open("C:/some.csv", "rb") as csvfile:
    reader = csv.reader(csvfile)
    for line in reader:

with arcpy.da.UpdateCursor(fc, ("FIELD2UPDATE")) as update:
    for row in update:
        random = random.randint(0, 49)
        value = csv_rows[random]
        row[0] = str(value)

Edited to add updateRow

share|improve this answer
I would do random = random.randint(0, len(csv_rows)-1) instead, in case the CSV grows or shrinks. – nmpeterson Apr 3 '14 at 17:58
Yeah, that would definitely be a good idea, but I like @Aarons use of random.choice() – ianbroad Apr 3 '14 at 18:28

Your Answer


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.