简体   繁体   English

根据 pandas 的其他列在列中添加随机值

[英]Adding random values in column depending on other columns with pandas

I have a dataframe with the Columns "OfferID", "SiteID" and "CatgeoryID" which should represent an online ad on a website.我有一个 dataframe 列“OfferID”、“SiteID”和“CatgeoryID”,它们应该代表网站上的在线广告。 I then want to add a new Column called "NPS" for the net promoter score.然后,我想为净推荐值添加一个名为“NPS”的新列。 The values should be given randomly between 1 and 10 but where the OfferID, the SideID and the CatgeoryID are the same, they need to have the same value for the NPS.这些值应在 1 到 10 之间随机给出,但在 OfferID、SideID 和 CatgeoryID 相同的情况下,它们需要具有相同的 NPS 值。 I thought of using a dictionary where the NPS is the key and the pairs of different IDs are the values but I haven't found a good way to do this.我想过使用一个字典,其中 NPS 是键,不同的 ID 对是值,但我还没有找到一个好的方法来做到这一点。

Are there any recommendations?有什么建议吗?

Thanks in advance.提前致谢。 Alina阿丽娜

The easiest would be first to remove all duplicates;最简单的方法是首先删除所有重复项; you can do this using:你可以这样做:

uniques = df[['OfferID', 'SideID', 'CategoryID']].drop_duplicates(keep="first")

Afterwards, you can do something like this (note that your random values are not uniques):之后,您可以执行以下操作(请注意,您的随机值不是唯一的):

uniques['NPS'] = [random.randint(0, 100) for x in uniques.index]

And then:接着:

df = df.merge(uniques, on=['OfferID', 'SideID', 'CategoryID'], how='left')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM