【发布时间】:2017-08-16 06:45:37
【问题描述】:
我是新手,我已经尝试查看了几个帖子,但似乎无法使其正常工作...我确定是我的问题。
试图合并和浓缩两个数据集,一个具有购买水果的日期和重量,另一个具有历史每日价格。我正在尝试将这些内容浓缩为每周一次。
我试图创建 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
我感觉这可以比我尝试的方法简单得多,因此非常感谢任何帮助。
提前谢谢你, 我
【问题讨论】:
标签: python-3.x pandas dataframe merge datetimeindex