情況:
scope = np.arange(0,1000)
我想要一個帶有分布的熊貓系列:
10% 亂數從 0 到 200
10% 亂數從 201 到 500
80% 亂數從 501 到 999
熊貓系列中的數字未排序
uj5u.com熱心網友回復:
假設你想要一個長度為 100 的系列,你可以這樣做:
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())
輸出
0 - 200 10
201 - 500 10
501 - 1000 80
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/342902.html
