0

I would like to create a unique sequential ID for each given values in the "Type" column but can't seem to make it work.

Current data frame:

    Item    Type    ID
0   Apple   Fruit   0
1   Orange  Fruit   1
2   Banana  Fruit   2
3   Peach   Fruit   3
4   Cheese  Dairy   0
5   Milk    Dairy   1
6   Chicken Meat    0
7   Pork    Meat    1
8   Beef    Meat    2

Desired data frame:

    Item    Type    ID
0   Apple   Fruit   0
1   Orange  Fruit   1
2   Banana  Fruit   2
3   Peach   Fruit   3
4   Cheese  Dairy   0
5   Milk    Dairy   1
6   Chicken Meat    0
7   Pork    Meat    1
8   Beef    Meat    2

I've tried to set_index and creating a separate column that indicating "Type" value change but was not able to create the desired format. Any help is appreciated.

Itamar Mushkin
  • 2,803
  • 2
  • 16
  • 32
sangurocactus
  • 63
  • 1
  • 5
  • 1
    Please post your dataframe directly rather than the image of it. Besides, how do you want to handle in case of identical _Item_? – Chris Jul 10 '19 at 04:44
  • Sorry Chris, the images didn't come out as expected. I will post the data frame directly. Tawab_Shakeel solved my problem below. Thanks! – sangurocactus Jul 10 '19 at 16:59

1 Answers1

3

try using cumcount()

df = pd.DataFrame(data={"Type":["Fruit","Fruit","Dairy","Meat","Meat"],
                        "Item":["Apple","Orange","Chesse","Pork","Beef"]})

df["ID"] = df.groupby(['Type']).cumcount()
print(df)
    Type    Item  ID
0  Fruit   Apple   0
1  Fruit  Orange   1
2  Dairy  Chesse   0
3   Meat    Pork   0
4   Meat    Beef   1

I hope it would solve your problem

tawab_shakeel
  • 3,701
  • 10
  • 26