Python 在dataframe中创建累积计数列
我有一个与此类似的数据帧设置Python 在dataframe中创建累积计数列,python,pandas,dataframe,Python,Pandas,Dataframe,我有一个与此类似的数据帧设置 **Person Value** Joe 3 Jake 4 Patrick 2 Stacey 1 Joe 5 Stacey 6 Lara 7 Joe 2 Stacey 1 我需要创建一个新的列“x”,它记录每个人的名字到目前为止在列表中出现的次数 预期产出: **Person Value** **x** Joe 3
**Person Value**
Joe 3
Jake 4
Patrick 2
Stacey 1
Joe 5
Stacey 6
Lara 7
Joe 2
Stacey 1
我需要创建一个新的列“x”,它记录每个人的名字到目前为止在列表中出现的次数
预期产出:
**Person Value** **x**
Joe 3 1
Jake 4 1
Patrick 2 1
Stacey 1 1
Joe 5 2
Stacey 6 2
Lara 7 1
Joe 2 3
Stacey 1 3
到目前为止,我所做的只是创建一个总计数,这不是我想要的
非常感谢您提供的任何帮助
df['x']=df.groupby('Person').cumcount()+1
使用groupby+cumcount