Skip to content Skip to sidebar Skip to footer

Python: Select Random Values For A Column From Csv

I have a problem to print random values from a csv for a given column name/index (my second day in Python world :) ) I have so far managed to write the following - #!/usr/bin/pytho

Solution 1:

Here is an unrelated example to show you how the shuffle and pop methods can be used:

from random import shuffle

a = [1,2,3,4,5]
shuffle(a)
print a
[5,1,4,2,3]
print a.pop()
3print a
[5,1,4,2]

The pop method without any arguments deletes the last element from a list and then returns it. However, since you are shuffling the list before hand, you will get a random sequence everytime.

Solution 2:

From what I understand, you want to do this:

  1. Read a CSV file with an unknown number of rows;
  2. Gather all the items in a given column, say column 2;
  3. Choose at random one row from that column.

If that is correct, it is fairly easy to do.

Suppose we have a CSV file like so:

1,2,3,4
5,6,7,8
9,10,11,12
13,14,15,16

Usually you would deal with a CSV file row by row. Since you want all the data from a column, you need to read the entire file before you have a set of data you can work with since the total number of rows is not known.

Here is a way:

import csv

col=2
with open(fn, 'r') as f:
    reader=csv.reader(f)
    data=[row[col] for row in reader]

print data
# ['3', '7', '11', '15']

Then if you want a single random number out of that list, use random.choice(data)


If you want to shuffle all the items in that column, use random.shuffle(data) then print it as a column using something like print '\n'.join(data) if all the elements of data are strings.

Solution 3:

Thanks @dawg, @sshashank124 and others -

here is the code -

#!/usr/bin/pythonimport csv   # This will help us reading csv formated files.import random # random method

col=2withopen('<filename>','r') as f:
        reader=csv.reader(f)
        data=[row[col] for row in reader]

from random import shuffle

shuffle(data)

print'\n'.join(data[:100])

f.close();

It is giving me output in the form of a column.

I am going to try to write it as a function and add other features next. I might start a separate thread for that.

Post a Comment for "Python: Select Random Values For A Column From Csv"