Home > database >  pandas: generate numbers from given bins
pandas: generate numbers from given bins

Time:10-31

Situation:

scope = np.arange(0,1000)  

I want a pandas series with distribution:

10% random numbers from 0 to 200
10% random numbers from 201 to 500
80% random numbers from 501 to 999

Numbers in pandas series unsorted

CodePudding user response:

Assuming you want a Series of length 100, you could do:

import numpy as np
import pandas as pd

np.random.seed(42)

length = 100
low = np.random.randint(0, 201, length // 10)  # 10%
mid = np.random.randint(201, 501, length // 10) # 10%
high = np.random.randint(501, 1000, 8 * (length // 10)) # 80%

# shuffle the data to make un-ordered across bins
data = np.concatenate([low,  mid, high])
np.random.shuffle(data)

res = pd.Series(data=data)

# just to verify
print("0 - 200", (res < 201).sum())
print("201 - 500", ((res > 200) & (res < 501)).sum())
print("501 - 1000", ((res > 501) & (res < 1000)).sum())

Output

0 - 200 10
201 - 500 10
501 - 1000 80
  • Related