Let's take a sample dataframe :
df = pd.DataFrame({"Name": ["Alan","Alan","Kate","Kate","Brian"],
"Shop" :["A","B","C","A","B"],
"Amount":[4,2,1,3,5]})
Name Shop Amount
0 Alan A 4
1 Alan B 2
2 Kate C 1
3 Kate A 3
4 Brian B 5
First expected output :
I would like to create a new dataframe from df having :
- as columns all the possible values in the column
Shopand the columnName - as index all the possible values in the column
Shop, repeated for each value in column `Name' - as values the value in the column
Ă€mountmatching with the columnsNameandShop
Expected output :
A B C Name
A 4 2 0 Alan
B 4 2 0 Alan
C 4 2 0 Alan
A 3 0 1 Kate
B 3 0 1 Kate
C 3 0 1 Kate
A 0 5 0 Brian
B 0 5 0 Brian
C 0 5 0 Brian
Second expected output :
It's almost the same as the first expected output. The only difference is that the value is the one that match with the index (and not column Name) and the column Shop.
Expected output :
A B C Name
A 4 4 4 Alan
B 2 2 2 Alan
C 0 0 0 Alan
A 3 3 3 Kate
B 0 0 0 Kate
C 1 1 1 Kate
A 0 0 0 Brian
B 5 5 5 Brian
C 0 0 0 Brian
Thanks to this post, I tried several scripts using pivot_table and pivot but I didn't reach my expected outputs. Would you know please how to do ?