vendredi 17 septembre 2021

Assign Random Number between two value conditionally

I have a dataframe:

df = pd.DataFrame({ 
    'Prod': ['abc', 'qrt', 'xyz', 'xam', 'asc', 'yat'], 
    'Line': ['Revenues','EBT', 'Expenses', 'Revenues', 'EBT', 'Expenses'],
    'AGE': ['35-44', '20-34', '35-44', '35-44', '45-70', '35-44']})

I wish to replace the values in the Age column by integers between two values. So, for example, I wish to replace each value with age range '35-44' by a random integer between 35-44.

I tried:

df.loc[df["AGE"]== '35-44', 'AGE'] = random.randint(35, 44)  

But it picks the same value for each row. I would like it to randomly pick a different value for each row.

I get:

df = pd.DataFrame({ 
    'Prod': ['abc', 'qrt', 'xyz', 'xam', 'asc', 'yat'], 
    'Line': ['Revenues','EBT', 'Expenses', 'Revenues', 'EBT', 'Expenses'],
    'AGE': ['38', '20-34', '38', '38', '45-70', '38']})

But I would like to get something like the following. I don't much care about how the values are distributed as long as they are in the range that I assign

df = pd.DataFrame({ 
    'Prod': ['abc', 'qrt', 'xyz', 'xam', 'asc', 'yat'], 
    'Line': ['Revenues','EBT', 'Expenses', 'Revenues', 'EBT', 'Expenses'],
    'AGE': ['36', '20-34', '39', '38', '45-70', '45']})



Aucun commentaire:

Enregistrer un commentaire