How to group by a dataframe and concatenate by a column
Question:
I have a df with the following structure:
Store
Sku
Value
1
A
20
2
A
20
1
B
10
2
B
25
And I have to transform it, so that the stores with the same sku and same value, end up concatenated in a cell with a "-" separator like the following.
Store
Sku
Value
1 – 2
A
20
1
B
10
2
B
25
How can I approach this?
Answers:
Groupby the sku and value and then list the store
df.groupby(['Sku','Value'])['Store'].apply(list).reset_index()
I have a df with the following structure:
Store | Sku | Value |
---|---|---|
1 | A | 20 |
2 | A | 20 |
1 | B | 10 |
2 | B | 25 |
And I have to transform it, so that the stores with the same sku and same value, end up concatenated in a cell with a "-" separator like the following.
Store | Sku | Value |
---|---|---|
1 – 2 | A | 20 |
1 | B | 10 |
2 | B | 25 |
How can I approach this?
Groupby the sku and value and then list the store
df.groupby(['Sku','Value'])['Store'].apply(list).reset_index()