Python: 创建一个新列,用来记录另一列中发生的次数。

huangapple go评论90阅读模式
英文:

Python: Create new column that numbers how many occurrences has taken place from another column

问题

关于我所提到的基本理解,frequency 列是我尝试创建的,基于在给定行之前出现的水果次数。

水果 频率 日期
苹果 1 8:00
香蕉 1 9:00
橙子 1 10:00
苹果 2 11:00
苹果 3 12:00
橙子 2 1:00

我尝试了以下代码,但无法使其正常工作:

df['Frequency'] = df.groupby(['fruit', 'date']).cumcount()

图像参考

英文:

Just for a basic understanding of what I am referring too, the frequency column is what I am trying to create, based on the number of times fruits has appeared prior to that given row

Fruit Frequency Date
Apple 1
Banana 1
Orange 1
Apple 2
Apple 3
Orange 2

I tried df['Frequency']=df.groupby['fruit', 'date'].cumcount() but could not get it to work

答案1

得分: 3

IIUC:

newdf = df.assign(Frequency=df.groupby('Fruit').cumcount() + 1)
>>> newdf
    Fruit  Frequency  Date
0   Apple          1   NaN
1  Banana          1   NaN
2  Orange          1   NaN
3   Apple          2   NaN
4   Apple          3   NaN
5  Orange          2   NaN
英文:

IIUC:

newdf = df.assign(Frequency=df.groupby('Fruit').cumcount() + 1)
>>> newdf
    Fruit  Frequency  Date
0   Apple          1   NaN
1  Banana          1   NaN
2  Orange          1   NaN
3   Apple          2   NaN
4   Apple          3   NaN
5  Orange          2   NaN

huangapple
  • 本文由 发表于 2023年3月4日 09:41:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75633166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定