[英]Is there any quick way to do the following in sql or python?
我有一个大小为1TB
的数据集,其中包含 3 列和大约 200 亿行。 我想以某种随机顺序将这些数据分成大约 80/20 块的两个子数据。 但是,这两个数据应该是非重叠的,这意味着一个块中的条目不应出现在另一个块中。 一个块的一列中的条目不应出现在另一块的任何列中。例如,假设示例数据是:
fruit apple seeds
vegetable carrot yellow
crops fruit lettuce
green onion vegetable
lettuce red health
两个子数据可以是
fruit apple seeds
crops fruit lettuce
lettuce red health
和
vegetable carrot yellow
green onion vegetable
对于如此大的数据,有什么有效的方法可以做到这一点吗?
您可以遍历文件并根据您布置的比例将行随机分配给 sub-data-1 和 sub-data-2。
import random
with open('large_file', 'r') as lf,
open('s1', 'w') as s1, open('s2', 'w') as s2:
for line in lf:
if random.random() < 0.8:
s1.write(line)
else:
s2.write(line)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.