我有一個非常大的數據框(大約1.1M行),我試圖對它進行採樣。從python中的大型數據框中快速採樣大量的數據
我有一個索引列表(約70,000個索引),我想從整個數據框中選擇。
這是我用盡爲止,但所有這些方法都服用了太多的時間:
方法1 - 使用大熊貓:
sample = pandas.read_csv("data.csv", index_col = 0).reset_index()
sample = sample[sample['Id'].isin(sample_index_array)]
方法2:
我試着寫所有采樣的行到另一個csv。
f = open("data.csv",'r')
out = open("sampled_date.csv", 'w')
out.write(f.readline())
while 1:
total += 1
line = f.readline().strip()
if line =='':
break
arr = line.split(",")
if (int(arr[0]) in sample_index_array):
out.write(",".join(e for e in (line)))
任何人都可以請建議一個更好的方法嗎?或者我可以如何修改它以使其更快?
感謝
如果我理解你是對的,你可以將你的標記轉化爲一個熊貓索引對象。然後將對象饋入DataFrame中直接切片。 – pylang