pandas.DataFrame.sample随机抽样

  1. 函数名及功能

该函数主要用于在数据集中取部分数据
DataFrame.sample(n=None, frac=None, replace=False, weights=None, random_state=None, axis=None)[source]

  1. 参数介绍
    pandas.DataFrame.sample随机抽样_第1张图片

  2. 使用举例

import pandas as pd
dic1={
     'A':[1,2,3,4,5],'B':[4,5,6,7,8],'C':[7,5,7,9,2]}
df1 = pd.DataFrame(dic1,index=['a','b','c','d','e',])
print('data:\n',df1)
data1=df1.sample(n=3,replace=False,random_state=None,axis=0)
print('\nsample:\n',data1)
data:
    A  B  C
a  1  4  7
b  2  5  5
c  3  6  7
d  4  7  9
e  5  8  2

sample:
    A  B  C
e  5  8  2
a  1  4  7
c  3  6  7

参考链接:https://www.cnblogs.com/webRobot/p/11484648.html

你可能感兴趣的:(python,pandas)