![](/img/trans.png)
[英]How to create a new dataframe row with a new column for every combination of other two columns?
[英]how can I create a new column with two columns combination?
我想要一個新列,其中包含 user_id 和 artist_id 相同的次數,例如,如果 user_id = 0,並且 artist_id = 10,並且它發生了 5 次,我想將數字 5 存儲在 5 行的列中發生這種情況。 此代碼為我提供了值,但我無法存儲它。
treino.groupby(['user_id', 'artist_id']).count()
IIUC 你需要一列代表每行中每個組的大小。 然后你需要使用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.