0

I want to create a SKU number depending on the article number like the following:

article_nr  SKUid
0   101      1
1   101      2
2   101      3
3   152      1
4   152      2
5   160      1
6   170      1

Can somebody help me with my problem?

My idea was to create something like:

for i in range(len(df01)): 
    while True: 
        if df01['article_nr'][i+1] == df01['article_nr'][i]:
            df01['SKUid'][i+1] = range(1, len(df01) + 1)
        else: 
            df01['SKUid'][i] = 1

is there a smart and efficient way?

Thank you all in advance!

Korollar
  • 21
  • 1
  • 5

2 Answers2

1

You can use .groupby() with .transform():

df['SKUid'] = df.groupby('article_nr').transform(lambda x: range(1, len(x)+1))

print(df)

Prints:

   article_nr  SKUid
0         101      1
1         101      2
2         101      3
3         152      1
4         152      2
5         160      1
6         170      1
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
1

You can use cumcount() for this.

df = pd.DataFrame({'article_nr':[101, 101, 101, 152, 152, 160, 170]})
df['SKU'] = df.groupby(['article_nr']).cumcount()+1
Aritesh
  • 1,985
  • 1
  • 13
  • 17