0

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
1
  • Questions seeking debugging help ("why isn't this code working?") should include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it as formatted text in the question itself. Questions without a clear problem statement are not useful to other readers. See: minimal reproducible example. Commented Dec 10, 2020 at 15:00

1 Answer 1

1

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)]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.