0

I want to enumerate the Customer-Assessor group in ascending order. Restarting every Client. Could you help me how to do it? I've tried using the groupby function, however, I'm not getting it to be restarted for each client.

Here is an example of the dataframe:

Date Customer Advisor
01/01/2023 A AA
02/01/2023 A BB
03/01/2023 A CC
04/01/2023 A AA
01/01/2023 B AA
02/01/2023 B BB
03/01/2023 B AA
04/01/2023 B CC

Follow the expected result: The date is important, but the dataframe is already sorted by date.

Date Customer Advisor Customer-Advisor
01/01/2023 A AA 1
02/01/2023 A BB 2
03/01/2023 A CC 3
04/01/2023 A AA 1
01/01/2023 B AA 1
02/01/2023 B BB 2
03/01/2023 B AA 1
04/01/2023 B CC 3

Thank you

haraujo
  • 33
  • 7

2 Answers2

1

With pd.Series.factorize to encode Advisor values as categorical variables:

df['Customer-Advisor'] = np.ravel([g['Advisor'].factorize()[0] + 1 
                                   for _, g in df.groupby('Customer', sort=False)])

          Date Customer Advisor  Customer-Advisor
0  01/01/2023        A       AA                 1
1  02/01/2023        A       BB                 2
2  03/01/2023        A       CC                 3
3  04/01/2023        A       AA                 1
4  01/01/2023        B       AA                 1
5  02/01/2023        B       BB                 2
6  03/01/2023        B       AA                 1
7  04/01/2023        B       CC                 3
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
0

It looks like you want a unique ID for each Advisor.

df["group_id"] = df.groupby(["Advisor"]).ngroup()

This starts the IDs at 0, but you can just add one to the column if you really care about that.

If you wanted to generate a unique ID for multiple groups (e.g. Customer AND Advisor), simply add to the list in groupby.

df["group_id"] = df.groupby(["Customer", "Advisor"]).ngroup()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
stressed
  • 328
  • 2
  • 7