I have the following pandas data frame:
import pandas as pd
data = dict(store=['A', 'B', 'B'], color=['red', 'black', 'black'], size=['small', 'medium', 'small'], quantity=[2, 4, 1])
df = pd.DataFrame(data)
which looks like this:
store color size quantity
0 A red small 2
1 B black medium 4
2 B black small 1
What I want to do is the pivot
function to create a new data frame that uses the store
column as a header and the the quantity
value for each of the unique row values of the color
and size
columns. So it should look like this:
A B
black 0 5
red 2 0
small 2 1
medium 0 4
Can this be done? Something like df.pivot(columns='store')
seems to be the way to go ...