'python random_sample for stock weightage and ensuring weightage are unique

I am currently using random_sample to generate weightage allocation for a portfolio of 3 stocks where each row values add up to 1.00, and the minimum allocation for each stock is 0.05.

weightage=[]
n = 0
while n < 10:
    weights = np.random.random_sample(3)
    weights  = weights/ np.sum(weights)
    if any(i < 0.05 for i in weights):
        continue
    n += 1
    weightage.append(weights)

0.39055438 0.44055996 0.16888567]
[0.22401792 0.26961926 0.50636282]
[0.67856154 0.21523207 0.10620639]
[0.33449127 0.36491387 0.30059486]
[0.55274192 0.23291811 0.21433997]
[0.20980909 0.38639029 0.40380063]
[0.24600751 0.199761   0.5542315 ]
[0.50743661 0.26633377 0.22622962]
[0.1154567  0.36803903 0.51650427]
[0.29092731 0.34675988 0.36231281]

Is there anyway to ensure each row is unique and does not repeat itself? To ensure that each portfolio have a unique weightage allocation.



Solution 1:[1]

Use this

# convert your data into dataframe with pandas
df = pd.DataFrame(weightage)

# This below line drops the duplicates(drops whole row if it repeats)
df = df.drop_duplicates()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Siva Reddy