我是一个新手,我已经尝试查看几个帖子,但似乎无法让它发挥作用......我肯定是我的问题。
试图合并和浓缩两个数据集,一个具有购买水果的日期和重量,另一个具有历史每日价格。我正在尝试将这些内容浓缩为每周一次。
我试图从中创建的原始数据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
我感觉这可以比我尝试的方法简单得多,因此非常感谢任何帮助。
提前谢谢你,我