Reputation: 91
I have the following dataframe:
Item | Produce Group |
---|---|
Mango | Fruit |
Spinach | Vegetable |
Peach | Fruit |
Squash | Vegetable |
Orange | Fruit |
Cauliflower | Vegetable |
How do I create the following dataframe from the previous dataframe?
Item | Produce Group |
---|---|
Mango | Fruit1 |
Spinach | Vegetable1 |
Peach | Fruit2 |
Squash | Vegetable2 |
Orange | Fruit3 |
Cauliflower | Vegetable3 |
Upvotes: 0
Views: 30
Reputation: 17814
You can groupby
and cumcount
elements in each group:
df['Produce Group'] + df.groupby('Produce Group').cumcount().add(1).astype(str)
Output:
0 Fruit1
1 Vegetable1
2 Fruit2
3 Vegetable2
4 Fruit3
5 Vegetable3
Upvotes: 2