简体   繁体   English

如何创建一个包含两列组合的新列?

[英]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.

相关问题 如何为其他两列的每个组合创建一个带有新列的新数据框行? - How to create a new dataframe row with a new column for every combination of other two columns? 如何基于组合1和许多列在Pandas DataFrame中创建新列 - How to create a new column in Pandas DataFrame based on a combination 1 and many columns 如何创建新列以跟踪数据框中两列之间的任何更改? - How can I create a new column to track any changes between two columns in a dataframe? 如何根据来自其他两列的值的分组总和创建新的值列? - How can I create a new column of values based on the grouped sum of values from two other columns? 如何创建根据其他两列(根据位置)计算出的新列 - How can I create a new column calculated from two other columns (based on location) 如何从 dataframe 中包含列表的列创建新列 - How can I create a new columns from a column with lists in a dataframe 如何连接两个 pandas 列并使用该行创建一个新列? - How can I concat Two pandas columns and create a new with the row? 如何基于两个不同的列创建列目标? - How can I create a column target based on two different columns? 在分组数据框中创建包含两列对角线组合的新列 - Create new column containing diagonal combination of two columns in a grouped data frame 我有一个包含两列的数据框,“主要一个”和“主要两个”。 如何创建一个新专栏“专业”? - I have a dataframe with two columns, “major one” and “major two”. How can I make a new column “majors”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM