英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论