Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Pandas Sampling and Random Data

Pandas Sampling and Random Data

Pandas provides rich random sampling capabilities: you can randomly select samples from a dataset as needed and also generate random data.


Random sampling

The sample method

Example

import

pandas

as

pd

import

numpy

as

np

Create a large dataset

df

=

pd.

DataFrame

(

{

   

"ID"

:

range

(

1

,

1001

)

,

   

"Value"

: np.

random

.

randn

(

1000

)

}

)

Randomly select 5 rows

print

(

"5 randomly selected rows:"

)

print

(

df.

sample

(

5

)

)

print

(

)

Select 30% of the data

print

(

"30% randomly selected:"

)

print

(

df.

sample

(

frac

=

0.3

)

)

print

(

)

Sampling with replacement

print

(

"5 rows sampled with replacement:"

)

print

(

df.

sample

(

5

,

replace

=

True

)

)

Set the random seed

Example

import

pandas

as

pd

import

numpy

as

np

Set a random seed to ensure reproducible results

np.

random

.

seed

(

42

)

df

=

pd.

DataFrame

(

{

   

"ID"

:

range

(

1

,

11

)

,

   

"Value"

: np.

random

.

randn

(

10

)

}

)

print

(

"Using random seed 42:"

)

print

(

df.

sample

(

3

)

)

Running again with the same seed produces the same result

print

(

"\nRunning again with the same seed:"

)

np.

random

.

seed

(

42

)

print

(

df.

sample

(

3

)

)


Random data generation

Example

import

pandas

as

pd

import

numpy

as

np

Generate random integers

print

(

"Random integers [0, 10):"

)

print

(

pd.

Series

(

np.

random

.

randint

(

0

,

10

,

5

)

)

)

print

(

)

Generate random floats

print

(

"Random floats [0, 1):"

)

print

(

pd.

Series

(

np.

random

.

random

(

5

)

)

)

print

(

)

Generate normally distributed random numbers

print

(

"Normal distribution N(0, 1):"

)

print

(

pd.

Series

(

np.

random

.

randn

(

5

)

)

)

print

(

)

Specify the mean and standard deviation

print

(

"Normal distribution N(10, 2):"

)

print

(

pd.

Series

(

np.

random

.

normal

(

10

,

2

,

5

)

)

)

print

(

)

Random choice

choices

=

[

"A"

,

"B"

,

"C"

,

"D"

]

print

(

"Random choice:"

)

print

(

pd.

Series

(

np.

random

.

choice

(

choices

,

10

)

)

)


Train/test split

Example

import

pandas

as

pd

import

numpy

as

np

Mock dataset

df

=

pd.

DataFrame

(

{

   

"feature1"

: np.

random

.

randn

(

100

)

,

   

"feature2"

: np.

random

.

randn

(

100

)

,

   

"target"

: np.

random

.

choice

(

[

0

,

1

]

,

100

)

}

)

Split into training and test sets (80% / 20%)

train

=

df.

sample

(

frac

=

0.8

,

random_state

=

42

)

test

=

df.

drop

(

train.

index

)

print

(

f

"Training set: {len(train)} rows"

)

print

(

f

"Test set: {len(test)} rows"

)

print

(

)

Stratified sampling (preserving the target variable ratio)

print

(

"Stratified sampling:"

)

from

sklearn.

model_selection

import

train_test_split

Requires scikit-learn: pip install scikit-learn

train, test = train_test_split(df, test_size=0.2, stratify=df["target"])