这似乎是一个简单的数据操作。但我被困在这一点上。
我有一个营销活动的推荐数据集。
Masteruserid content
1 100
1 101
1 102
2 100
2 101
2 110现在,对于每个用户,我们至少推荐5个内容。例如,Masteruserid 1有三个推荐,我想从全局查看的内容中随机选择剩下的两个,这是一个单独的数据集(列表)。然后,我还必须检查重复项,以防随机挑选的数据已经存在于原始数据集中。
global_content
100
300
301
101实际上,我在4000+的Masteruserid附近,现在我需要帮助如何开始处理这个问题。
发布于 2016-08-23 22:27:46
def add_content(df, gc, k=5):
n = len(df)
gcs = set(gc.squeeze())
if n < k:
choices = list(gcs.difference(df.content))
mc = np.random.choice(choices, k - n, replace=False)
ids = np.repeat(df.Masteruserid.iloc[-1], k - n)
data = dict(Masteruserid=ids, content=mc)
return df.append(pd.DataFrame(data), ignore_index=True)
gb = df.groupby('Masteruserid', group_keys=False)
gb.apply(add_content, gc).reset_index(drop=True)

发布于 2016-08-24 00:47:36
尝试这个,使用this as recs列表,
df2['global_content']
0 100
1 300
2 301
3 101
4 400
5 500
6 401
7 501
recs = pd.DataFrame()
recs['content'] = df.groupby('Masteruserid')['content'].apply(lambda x: list(x) + np.random.choice(df2[~df2.isin(list(x))].dropna().values.flatten(), 2, replace=False).tolist())
recs
content
Masteruserid
1 [100, 101, 102, 300.0, 301.0]
2 [100, 101, 110, 501.0, 301.0]https://stackoverflow.com/questions/39103090
复制相似问题