使用groupby条件遍历Pandas DataFrame中的一列

时间:2018-10-18 21:35:38

标签: pandas iteration

我在pandas DataFrame中有一个数据集。数据按[“ Customer_Id”,“广告系列”]排序。 我的目标虽然是向groupby函数添加另一步。对于每个广告系列,都有批次,新批次由New_rank == 1表示。

How my data looks like now.

我想添加一列=“ Occurence”,为每个Customer_Id和广告系列分配一个编号为“ batch”。

在这种情况下,需要这样的输出:

enter image description here

任何想法将不胜感激!

1 个答案:

答案 0 :(得分:0)

使用cumsum

df['Occurence'] = df.groupby(['CustomerId','Campaign'])['New_rank'].cumsum()

输出:

    CustomerId  Campaign  New_rank  Occurence
0            1         1         1          1
1            1         1         0          1
2            1         1         1          2
3            1         2         1          1
4            1         2         1          2
5            2         1         1          1
6            2         1         0          1
7            2         1         0          1
8            2         2         1          1
9            2         3         1          1
10           2         3         0          1
相关问题