O'Reilly logo

Learning pandas - Second Edition by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Executing random sampling of data

Random sampling is a process of selecting values from a data sample at random positions. From pandas 0.19.2 on, this functionality has been added to the pandas Series and DataFrame objects, whereas in previous versions, you had to code this process on your own.

To demonstrate random sampling, let's start with the following DataFrame, representing four columns of 50 rows of random numbers:

We can take a sample of the data using the .sample() method while specifying the number of samples to retrieve. The following code samples three random rows:

An alternative form is to specify a percentage of the data to ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required