Reputation: 31
I want a string column of location values (for example Amsterdam, London, Paris) included in my dataset that already has 2 columns with only integer values. I don't know how to include it with a for loop.
My code looks like this:
with open(output_file_path, 'w') as csvfile:
csv_writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow(['timestamp', 'id', 'integercolumn1', 'integercolumn2'])
for i in range(100):
csv_writer.writerow([timestamp, 'demo-{}'.format(i), random.randint(0, 75), random.randint(4, 10), ])
The table/dataset looks now like this:
timestamp | ID | integercolumn1 | integercolumn2 |
2020-1 | 1 | 32 | 25 |
2020-1 | 2 | 45 | 14 |
But I want something like this. Thus random location also included
timestamp | ID | integercolumn1 | integercolumn2 | location
2020-1 | 1 | 32 | 25 | London
2020-1 | 2 | 45 | 14 | Paris
Upvotes: 0
Views: 42
Reputation: 620
can you have a list of locations and randomly select from the list like below.Is this what you looking for?
location = ['Paris','London']
with open('test', 'w') as csvfile:
csv_writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow(['timestamp', 'id', 'integercolumn1', 'integercolumn2','location'])
for i in range(10):
csv_writer.writerow(['demo-{}'.format(i), random.randint(0, 75),
random.randint(4, 10), location[random.randint(0, 1)]])
Upvotes: 1