Python:从csv中为列选择随机值 [英] Python: select random values for a column from csv

查看:358
本文介绍了Python:从csv中为列选择随机值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于从给定列名/索引的csv中打印随机值(我在Python世界中第二天:)),我遇到了问题

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/python

import csv   # This will help us reading csv formated files.

import random # This will random method

load_file= open('<filename>',"rb")

reader= csv.reader(load_file) #The reader method will put each line
                             # of the csv file into a list of columns

for row in reader:

        from random import shuffle
        shuffle(row[2])

        print row[2]


load_file.close();

它正在从文件的第三列中打印随机(随机)的值.

It is printing a shuffled (random) values from the third column in the file.

目标- .定义值的数量1000、2000、50000等. .值高度偏斜如何确保均匀分布?例如如果该列大部分为0&我想在输出中看到任意样本大小的两个值. .将此写入文件. (目前不紧急)

objectives - . define the number of values 1000,2000,50000 etc. . The values are highly skewed how to ensure uniform distribution ? e.g. if the column has got mostly 0s & few 1s I want to see both values in the output for any sample size. . write this into a file. (not urgent at this point)

我正在使用python 2.6.6

I am using python 2.6.6

推荐答案

感谢@ dawg,@ sshashank124等-

Thanks @dawg, @sshashank124 and others -

这是代码-

#!/usr/bin/python

import csv   # This will help us reading csv formated files.

import random # random method

col=2

with open('<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.

这篇关于Python:从csv中为列选择随机值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆