pandas对excel数据按行不重复随机抽样 之前要从excel表格按行随机抽取数据,使用python内置函数生成随机数的方法可能会产生重复行,学习pandas后发现几行就可以解决 import pandas as pd #读取excel表格,pandas直接转换为DataFrame frame = pd.read_excel('path') #path是excel文件保存路径 #sample方法随机抽取n行 samples = frame.sample(n) #写入新的excel sample.to_excel('new path') #保存抽样数据的路径