Currently have a dataframe like this:
| RepName | Hours | Reason |
|---|---|---|
| John | 4 | Research |
| John | 15 | Training |
| Matt | 6 | Project Labor |
| Matt | 10 | Training |
I want to transpose each reason as a column with the Hours values as values like so:
| RepName | Research | Training | Project Labor |
|---|---|---|---|
| John | 4 | 15 | 0 |
| Matt | 0 | 10 | 6 |
I tried to transpose and melting the dataframe and couldn’t figure it out. Still sort of new to this, any help would be appreciated.
>Solution :
You can do this with pd.pivot_table like so:
>>> df.pivot_table(index="RepName", columns="Reason", values="Hours", fill_value=0)
Reason Project Labor Research Training
RepName
John 0 4 15
Matt 6 0 10
Full example:
import pandas as pd
input_data = pd.DataFrame(
{
"RepName": ["John", "John", "Matt", "Matt"],
"Hours": [4, 15, 6, 10],
"Reason": ["Research", "Training", "Project Labor", "Training"],
}
)
output_data = pd.DataFrame(
{
"Project Labor": [0, 6],
"Research": [4, 0],
"Training": [15, 10],
},
index=["John", "Matt"],
)
pivoted = input_data.pivot_table(
index="RepName",
columns="Reason",
values="Hours",
fill_value=0,
)
assert output_data.equals(pivoted)