0

I am new to arcpy and am looking to generate 15 random point values, which are names from an existing attribute column into a new column. I must use Python in the field calculator. My existing column name is: CityNames which includes 350 cites, and my new column name is: randomCities.

 def RandomPts(randomCities):
 import random
 'return 15 random points'
 return random.choice(CityNames,15)
  • 1
    This sounds suspiciously like homework – Jason Scheirer Oct 14 '17 at 0:18
1

If I understand correctly, you just need to select 15 random cities from a list of 350 cities. Ideally, Select By Attributes tool would perfectly suit you, but it's in SQL. If you want to do this in the attribute table, here's my solution:

Pre-Logic Script Code:

import random
rndSample = random.sample(range(350), 15)
def randCities(id, city):
    if id in rndSample:
        return city
    else:
        return '-'

15 Random cities field:

randCities(!FID!, !CityNames!)

Now in a new column you'll have 15 random city names.

|improve this answer|||||
  • Not sure if you'll have exactly 15 rows populated by something <> '-'. Did you test it? – FelixIP Oct 14 '17 at 0:45
  • I believe I need to populate names for each of the rows in the column using the randomly selected 15 points. I am not quite sure how to do this. – Jennifer Oct 14 '17 at 0:54
  • @FelixIP yes, I tested it. The '-' sign is for other 335 cities not included in random sampling, as I assume we need just select 15 random rows out of 350. – Sergey Gulbin Oct 14 '17 at 21:56
  • 1
    @Jennifer Sorry, but I'm not sure what do you need exactly in the output. It would be very helpful if you would give an example of input attribute table and approximate desired output. – Sergey Gulbin Oct 14 '17 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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