I'm working with a dataframe that looks like this:
Client_ID Product_ID Cost
0 4 1 40
1 4 2 32
2 5 1 38
3 6 7 89
4 7 3 21
5 4 5 45
6 2 5 23
7 2 4 71
8 5 8 11
9 7 8 14
For each couple 'Client_ID, Product_ID' there is only one occurence/row in the dataframe.
I want to build a dataframe where the Product_ID is the index and where the the column names are the client names while the cost become the value in each cell, it would look like this:
Client_ID
Product_ID 1 2 3 4 5 6 7
1 x x x 40 38 x x
2 x x x 32 x x x
3 x x x x x x 21
4 x 71 x x x x x
5 x 23 x 45 x x x
6 x x x x x x x
7 x x x x x 89 x
8 x x x x 11 x 14
9 x x x x x x x
10 x x x x x x x
I tried to achieve this by doing this:
df.pivot(index='Product_ID', columns='Client_ID')
But it didn't work, I tried then making Product_ID the index first and then do the pivot:
df = df.set_index('Product_ID')
df.index.name = None
df.pivot(columns='Client_ID')
No success neither.
Does somebody know how to achieve such a thing?
Thank you for your help.
Edit
The Product_ID values are strings.
df.pivot(index='Product_ID', columns='Client_ID', values='Cost').reindex(columns=np.arange(1, df.Client_ID.max() + 1)).fillna('x')