Search code examples
pythonpandasstochastic

assign value with stochastic function in pandas


I'm trying to STOCHASTICALLY assign a fourth value (1 of 2 types of buddy) based on value of category value.

small df with randomly assigned values for 3 features: category, age and sex

        Unique_ID   Category    Age      Sex        Buddy  
0       0           2           11       male       NaN
1       1           3           7        female     NaN
2       2           1           4        male       NaN
3       3           2           20       male       NaN
4       4           1           19       female     NaN

i include code to generate df if helpful for answer

i've made a function to hard-coding the probabilities for np.random.choice, but am running into error message when applying assign_buddy function to df ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

columns = ['Unique_ID',  'Category', 'Age', 'Sex', 'Buddy']
df = pd.DataFrame(columns=columns)

Sexes = ['female', 'male']
df.Sex = np.random.choice(a=Sexes, size=n, p=[0.6, 0.4])

list_Category = [1,2,3,4]
df.Category = np.random.choice(a=list_category, size=n, p=[0.3, 0.4, 0.2, 0.1])

buddy_list = ['buddy_1', 'buddy_2']

def assign_buddy(Category_prob_list):
"""
takes in a Category value
return: Buddy
"""    
    if  df['Category'] == list_Category[0]:
        df['Buddy'] = np.random.choice(a=buddy_list, size=n, p=[0.1, 0.9])
        return df['Buddy']
    elif df['Category'] == list_Category[1]:
        df['Buddy'] = np.random.choice(a=buddy_list, size=n, p=[0.3, 0.7])
        return df['Buddy']
    elif df['Category'] == list_Category[2]:
        df['Buddy'] = np.random.choice(a=buddy_list, size=n, p=[0.7, 0.3])
        return df['Buddy']
    elif df['Category'] == list_Category[3]:
        df['Buddy'] = np.random.choice(a=buddy_list, size=n, p=[0.9, 0.1])
        return df['Buddy']
    else:
        pass
# should apply assign_buddy to each row in df
df['Category'].apply((assign_buddy))  

i have a dictionary of probabilities for assign_buddy, but can not figure out the map and apply logic despite all the documentation .

i've tried creating a function that returns probabilities from d to be passed to the argument p in np.random.choice, but it's not working.

# key is category label and values are probabilities for np.random.choice
d = {1: [0.1, 0.9], 2: [0.3, 0.7], 3: [0.7, 0.3], 4: [0.9, 0.1]}

any insight appreciated!


Solution

  • Try this

    n = 20
    columns = ['Unique_ID',  'Category', 'Age', 'Sex', 'Buddy']
    df = pd.DataFrame(columns=columns)
    
    list_category = [1,2,3,4]
    buddy_list = ['buddy_1', 'buddy_2']
    Sexes = ['female', 'male']
    df.Sex = np.random.choice(a=Sexes, size=n, p=[0.6, 0.4])
    df.Category = np.random.choice(list_category, size=n, p=[0.3, 0.4, 0.2, 0.1])
    
    d = {1: [0.1, 0.9], 2: [0.3, 0.7], 3: [0.7, 0.3], 4: [0.9, 0.1]}
    
    for val in list_category:
        sz = (df["Category"] == val).sum() # find the size for array to create
        # use `loc` to select places you want to replace
        df.loc[df["Category"] == val,'Buddy'] = np.random.choice(
                                                   buddy_list, sz, p=d[val])