【数据分析】从一个CSV文件中随机选取m行数据

CSV文件一共有m(4017277)条数据,随机取出n(10万)条数据,保存值另外CSV一个文件。

注意:数据类型是DataFrame

import random
from random import randint
 
oldf=open('thp_zbwd_bing_01_Del_abs50.csv','r',encoding='UTF-8')
newf=open('thp_zbwd_bing_01_Del_abs50_Random.csv','w',encoding='UTF-8')
n = 0
# sample(x,y)函数的作用是从序列x中,随机选择y个不重复的元素
resultList = random.sample(range(0,4017277),100000)

lines=oldf.readlines()
for i in resultList:
    newf.write(lines[i])
    
oldf.close()
newf.close()

猜你喜欢

转载自www.cnblogs.com/ITCSJ/p/11411149.html
今日推荐