我是一個新手,我試着看幾個帖子,似乎無法得到這個工作...我是問題,我敢肯定。熊貓TimeGrouper&合併datetimeindex
試圖合併和壓縮兩個數據集,一個具有購買水果的日期和重量,另一個具有歷史每日價格。我試圖將這些內容壓縮到每週一次。
的原始數據,我想從長相創建Weights
這樣的:
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
下面是我試圖創建一個數據幀的代碼,但返回一個系列:
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()
我希望將其與我創建的系列Prices
合併,該系列列出每週價格:
(datetimeindex) Product Price
2012-01-1 Strawberry 2.10
2012-01-1 Banana 0.55
2012-01-1 Apple 1.25
下面的代碼來創建Prices
,我用:
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()
最終數據框,我試圖讓將有每週的價格和我們買了多少金額週刊。它看起來是這樣的:
(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
我得到這個可以做的比我如何想簡單了很多的感覺,所以任何的幫助深表感謝。
謝謝你在前進, 我做這件事
如果投票人仍在看這篇文章,我想解決您的疑慮,並可能說服您刪除投票。如果可以的話,請讓我知道這篇文章有什麼問題。它沒有用嗎? – piRSquared