Say I have a pivoted dataframe of the form
Value Qty Code
Color Blue Green Red Blue Green Red Blue Green Red
Date
2017-07-01 0.0 1.1 0.0 0.0 12.0 0.0 0 abc 0
2017-07-03 2.3 1.3 0.0 3.0 1.0 0.0 cde abc 0
2017-07-06 0.0 0.0 1.4 0.0 0.0 1.0 0 0 cde
I am interested in resampling the Date into weekly frequency. I would like to perform the following transformation on each of the sub-columns of the major column, Value: max, Qty: sum, Code = last. In a normal non-MultiIndex dataframe, df, one would do the following via the agg() function.
df.resample('W').agg({"Value":"max", "Qty":"sum", "Code":"last"})
But when I try it with the pivoted dataframe, it doesn't like the keys. How would I do it in the case of multi-index dataframe without explicitly specifying all the sub-columns?
The expected output is
Value Qty Code
Color Blue Green Red Blue Green Red Blue Green Red
Date
2017-07-02 0.0 1.1 0.0 0.0 12.0 0.0 0 abc 0
2017-07-09 2.3 1.3 1.4 3.0 1.0 1.0 0 0 cde
To generate the above starting dataframe, use the following code
from collections import OrderedDict
import pandas as pd
table = OrderedDict((
("Date", ["2017-07-01", "2017-07-03", "2017-07-03", "2017-07-6"]),
('Color',['Green', 'Blue', 'Green', 'Red']),
('Value', [1.1, 2.3, 1.3, 1.4]),
('Qty', [12, 3, 1, 1]),
('Code', ['abc', 'cde', 'abc', 'cde'])
))
d = pd.DataFrame(table)
p = d.pivot(index='Date', columns='Color')
p.index = pd.to_datetime(p.index)
p.fillna(0, inplace=True)
EDIT: Added desired result.
EDIT 2: I have also tried to create a dictionary to feed into the agg() function but it's coming out with 4 levels of column headers.
dc = dict(zip(p.columns, map({'Value': 'max', 'Qty': 'sum', 'Code': 'last'}.get, [x[0] for x in p.columns])))
newp = p.resample('W').agg(dc)