I'm a newbie and I've tried looking at several posts and can't seem to get this to work... I'm the problem, I'm sure.
Trying to combine and condense two data sets, one has the date and weight of fruit purchased and the other has historical daily prices. I'm trying to condense these together to a weekly basis.
The raw data I'm trying to create Weights
from looks like this:
Date Product Weight
1-1-12 Strawberry 15
1-2-12 Bananna 56
1-2-12 Apple 98
1-5-12 Strawberry 115
1-5-12 Bananna 516
1-5-12 Apple 981
Here's the code I've tried to create a dataframe but returns a series:
df_GrossWeight = pd.read_csv('mydata.csv', encoding='utf-8')
df_GrossWeight_Indexed = df_GrossWeight.set_index(pd.DatetimeIndex(df_GrossWeight['Date']))
grouper = df_GrossWeight_Indexed.groupby([pd.TimeGrouper('W'),'Product'])
Weights = grouper['Weight'].sum()
I want to merge it with the series I created Prices
that lists the weekly prices:
(datetimeindex) Product Price
2012-01-1 Strawberry 2.10
2012-01-1 Banana 0.55
2012-01-1 Apple 1.25
Here's the code to create Prices
that I used:
df_Price = pd.read_csv('Price_Hist.csv')
df_Indexed = df_Price.set_index(pd.DatetimeIndex(df_Price['Date']), drop = True)
df_Price_Indexed = df_Indexed['Price']
Prices = df_Price_Indexed.resample('W').mean()
The end dataframe I'm trying to make will have the weekly prices and weekly sums of how much we bought. It will look like this:
(datetimeindex) Product Price Weight
2012-01-1 Strawberry 2.10 130
2012-01-1 Banana 0.55 572
2012-01-1 Apple 1.25 1079
I'm getting the feeling this can be done a lot simpler than how I'm trying, so any help is much appreciated.
Thank you in advance, Me