[英]how can I create a new column with two columns combination?
i want a new column that contains the amount of times user_id and artist_id are the same, for example if user_id = 0, and artist_id = 10, and it happens 5 times, i want to store number 5 in a column in the 5 rows in which this occurs.我想要一个新列,其中包含 user_id 和 artist_id 相同的次数,例如,如果 user_id = 0,并且 artist_id = 10,并且它发生了 5 次,我想将数字 5 存储在 5 行的列中发生这种情况。 This code gives me the value, but I can't store it.
此代码为我提供了值,但我无法存储它。
treino.groupby(['user_id', 'artist_id']).count()
IIUC you need a column that represents the size of each group in each row. IIUC 你需要一列代表每行中每个组的大小。 Then you need to use
groupby.transform
.然后你需要使用
groupby.transform
。
df["group_size"] = (
df.assign(group_size=1)
.groupby(["user_id", "artist_id"])["group_size"]
.transform("count")
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.