I have some data in a pandas dataframe, where the date column is the index, as follows:
date value A value B country category
ddmmyy A1 B1 US Red
ddmmyy A2 B2 US Red
ddmmyy A3 B3 UK Green
ddmmyy A4 B4 UK Green
ddmmyy A5 B5 US Red
ddmmyy A6 B6 US Red
There are multiple countries (other than UK, US) and multiple categories (other than red and green). The dates represent transactions and occur at an irregular frequency. I want to regroup the data as follows:
date UK US
mmyy num11 num21
mmyy num12 num22
mmyy num13 num23
mmyy num14 num24
Where
1. The dates are regular consecutive monthly dates
2. num11 for the UK is the weighted average of A1xB1, A2xB2 etc i.e. weighted average for a given month = sum {An x Bn}
in the given month, divided by sum of Bn in the given month. And likewise for US.
I have tried using e.g.
dataframe['Val A'].resample('M', how='sum')
Which resamples col A into monthlu buckets by summing the number e.g. A1, A2 etc. per month. But doesnt give me the weighted average I want per country.
Whats the best way to do this in Pandas/Python?