为了对我所指的内容有一个基本的了解,frequency列是我试图创建的列,它基于fruit在给定行之前出现的次数| 水果|频率|日期|| - ------|- ------|- ------|| 苹果|1个||| 香蕉|1个||| 橙子|1个||| 苹果|第二章||| 苹果|三个||| 橙子|第二章||我尝试了df['Frequency']=df.groupby['fruit', 'date'].cumcount(),但无法使其工作
frequency
df['Frequency']=df.groupby['fruit', 'date'].cumcount()
6kkfgxo01#
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
1条答案
按热度按时间6kkfgxo01#
IIUC: