I am trying to convert a column of values into separate columns using pandas in python. So I have columns relating to shops and their products and the number of products each shop has could be different. For example:
JavaScript
x
8
1
ShopId ShopCode ShopName ProductName
2
1 a Shop One x
3
1 a Shop One y
4
1 a Shop One z
5
2 b Shop Two 2x
6
2 b Shop Two 2y
7
3 c Shop Three 3x
8
What I am trying to achieve would look something like this:
JavaScript
1
5
1
ShopId ShopCode ShopName Product1 Product2 Product3
2
1 a Shop One x y z
3
2 b Shop Two 2x 2y
4
3 c Shop Three 3x
5
If there are any shops that have more than 3 products, I would need more columns to be created for them dynamically. Any ideas or suggestions are appreciated! Thanks!
Advertisement
Answer
Use GroupBy.cumcount
for counter with reshape by Series.unstack
:
JavaScript
1
13
13
1
c = ['ShopId','ShopCode','ShopName']
2
g = df.groupby(c).cumcount()
3
df1 = (df.set_index(c + [g])['ProductName']
4
.unstack(fill_value='')
5
.rename(columns=lambda x: f'Product{x + 1}')
6
.reset_index())
7
8
print (df1)
9
ShopId ShopCode ShopName Product1 Product2 Product3
10
0 a Shop One x y z
11
1 b Shop Two 2x 2y
12
2 c Shop Three 3x
13