I have a sample input dataframe:
| Name | Date | Score | Target | Difference |
|---|---|---|---|---|
| Jim | 2023-10-09 | 9 | 12 | 3 |
| Jim | 2023-10-16 | 13 | 16 | 3 |
| Andy | 2023-10-09 | 7 | 7 | 0 |
| Andy | 2023-10-16 | 5 | 20 | 15 |
Python code to create table:
import pandas as pd
df = pd.DataFrame({'Name':["Jim","Jim","Andy", "Andy"], 'Date':['2023-10-09', '2023-10-16', '2023-10-09', "2023-10-16"], 'Score':["9","13","7", "5"], 'Target':["12","16","7", "20"], 'Difference':["3","3","0", "15"]})
I want to transpose the above table by Name and have the rows be Date, Score, Target, and Difference. The desired output table is below:
| Date | Category | Jim | Andy |
|---|---|---|---|
| 2023-10-09 | Score | 9 | 7 |
| Target | 12 | 7 | |
| Difference | 3 | 0 | |
| 2023-10-16 | Score | 13 | 5 |
| Target | 16 | 20 | |
| Difference | 3 | 15 |
I tried doing this with the code below but it doesn't produce the desired transposed table grouping by Date and Category columns.
df_2 =df.T
df_2 produces the following output which is transposing "Date" column which I don't want.
