我是Python的新手,想要学习使用它的数据处理过程。我正在使用jupyter。
我有一个名为fle的文件,包含81,000行和89列。我想从中随机选择约100行。我怎么做?我一直在跟踪错误。
fle=pd.read_csv("C:\Users\Mine\Documents\ssample.csv", low_memory= False)
import random
sampl = random.sample(fle, 10)
我得到的错误是:
IndexError Traceback (most recent call last)
<ipython-input-37-fa4ec429f883> in <module>()
1 import random
2 #To take a sample of 10000 samples
----> 3 sampl = random.sample(fle, 10)
4 #pd.DataFrame(sampler).head(10)
C:\Users\E061921\AppData\Local\Continuum\Anaconda\lib\random.pyc in sample(self, population, k)
334 for i in xrange(k): # invariant: non-selected at [0,n-i)
335 j = _int(random() * (n-i))
--> 336 result[i] = pool[j]
337 pool[j] = pool[n-i-1] # move non-selected item into vacancy
338 else:
IndexError: list index out of range
答案 0 :(得分:1)
使用random.choice
代替示例。您可以使用csv.DictReader
来处理csv作为dicts列表
import csv
import random
random_rows = set()
with open("C:\Users\Mine\Documents\ssample.csv", "r") as csvfile:
reader = csv.DictReader(csvfile)
rows = [r for r in reader]
while len(random_rows) < 100:
random_rows.add(random.choice(rows))