在pandas中赋值随机函数

时间:2017-12-28 17:51:16

标签: python pandas stochastic

我正在尝试根据类别值的价值随意分配第四个值(两种类型的伙伴中的一个)。

小df,随机分配3个特征的值:类别,年龄和性别

        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

我包含生成df的代码,如果有帮助回答

我已经为np.random.choice的概率进行了硬编码,但在将assign_buddy函数应用于df时遇到错误信息 ValueError:Series的真值是不明确的。使用a.empty,a.bool(),a.item(),a.any()或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))  

我有一个assign_buddy概率字典,但无法弄清楚地图并应用逻辑,尽管所有文档。

我已经尝试创建一个函数,它返回来自d的概率,传递给np.random.choice中的参数p,但它不起作用。

# 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]}

任何洞察力都赞赏!

1 个答案:

答案 0 :(得分:0)

试试这个

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])