Mark every Nth row per group using pandas

Viewed 1267

I have a Dataframe with customer info with their purchase details. I am trying to add a new columns that indicates every 3rd purchase done by the same customer.

Given below is the Dataframe

customer_name,bill_no,date
Mark,101,2018-10-01
Scott,102,2018-10-01
Pete,103,2018-10-02
Mark,104,2018-10-02
Mark,105,2018-10-04
Scott,106,2018-10-21
Julie,107,2018-10-03
Kevin,108,2018-10-07
Steve,109,2018-10-02
Mark,110,2018-10-06
Mark,111,2018-10-02
Mark,112,2018-10-05
Mark,113,2018-10-05

I am writing to filter every 3rd purchase done by the same customer. So in this case, I would like to add a flag for the below bill_no

Mark,105,2018-10-04
Mark,112,2018-10-05

Basically every multiple of 3 bill generated for the same customer.

2 Answers

Using groupby.cumcount:

n = 3
df['flag'] = df.groupby('customer_name').cumcount() + 1
df['flag'] = ((df['flag'] % n) == 0).astype(int)

print(df)
   customer_name  bill_no        date  flag
0           Mark      101  2018-10-01     0
1          Scott      102  2018-10-01     0
2           Pete      103  2018-10-02     0
3           Mark      104  2018-10-02     0
4           Mark      105  2018-10-04     1
5          Scott      106  2018-10-21     0
6          Julie      107  2018-10-03     0
7          Kevin      108  2018-10-07     0
8          Steve      109  2018-10-02     0
9           Mark      110  2018-10-06     0
10          Mark      111  2018-10-02     0
11          Mark      112  2018-10-05     1
12          Mark      113  2018-10-05     0

If actually getting the indices are important, you should use groupby + apply with slicing on the index:

n = 3
idx = df.groupby('customer_name', group_keys=False).apply(
    lambda x: x.index[n-1::n].to_series())
# So you can query these rows easily.
df.loc[idx]

   customer_name  bill_no        date
4           Mark      105  2018-10-04
11          Mark      112  2018-10-05

Now, mark them using the indices:

df['flag'] = 0
df.loc[idx, 'flag'] = 1

df
   customer_name  bill_no        date  flag
0           Mark      101  2018-10-01     0
1          Scott      102  2018-10-01     0
2           Pete      103  2018-10-02     0
3           Mark      104  2018-10-02     0
4           Mark      105  2018-10-04     1
5          Scott      106  2018-10-21     0
6          Julie      107  2018-10-03     0
7          Kevin      108  2018-10-07     0
8          Steve      109  2018-10-02     0
9           Mark      110  2018-10-06     0
10          Mark      111  2018-10-02     0
11          Mark      112  2018-10-05     1
12          Mark      113  2018-10-05     0

If performance is important, use Sandeep's solution instead.

Related