sample() is used to generate a sampled random row or column from the caller’s data frame.
DataFrame.sample (n = None, frac = None, replace = False, weights = None, random_state = None, axis = None)
n: int value, Number of random rows to generate.
frac: Float value, Returns (float value * length of data frame values). frac cannot be used with n.
replace: Boolean value, return sample with replacement if True.
axis: 0 or ’row’ for Rows and 1 or ’column’ for Columns.
Return type: a new object of the same type as the caller.
To load CSV file to use, click here.
Example # 1: Random string from data frame
In this example, two random strings are generated by the .sample () method and compared later.
As shown in the output image, the two generated rows of random samples are different from each other.
Example # 2: Generating 25% of a sample of a data frame
In this example, 25% of a random sample of data is generated from a data frame.
As shown in the output image, the length of the generated sample is 25% of the data frame. Also the sample is generated randomly.