熊猫TimeGrouper&合并datetimeindex

时间:2017-03-23 15:38:52

标签: python-3.x pandas dataframe merge 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

我感觉这可以比我尝试的方式简单得多,所以非常感谢任何帮助。

提前谢谢你, 我

2 个答案:

答案 0 :(得分:2)

实现这一目标的一种方法是“轮流”。所有日期到最近的工作日。一旦你有了这个'圆形'日期。您可以加入这两个数据帧。

df['Date'] = pd.to_datetime(df['Date'])
df2['(datetimeindex)'] = pd.to_datetime(df2['(datetimeindex)'])

绕到最近的星期日

df2['Week_Sunday'] = df2['(datetimeindex)'] + pd.tseries.offsets.Week(weekday=6)
df['Week_Sunday'] = df.Date + pd.tseries.offsets.Week(weekday=6)

现在合并数据

df_all = pd.merge(df2, df, on = ['Week_Sunday', 'Product'])
print(df_all)

输出

  (datetimeindex)     Product  Price Week_Sunday       Date  Weight
0      2012-01-01  Strawberry   2.10  2012-01-08 2012-01-01      15
1      2012-01-01  Strawberry   2.10  2012-01-08 2012-01-05     115
2      2012-01-01      Banana   0.55  2012-01-08 2012-01-02      56
3      2012-01-01      Banana   0.55  2012-01-08 2012-01-05     516
4      2012-01-01       Apple   1.25  2012-01-08 2012-01-02      98
5      2012-01-01       Apple   1.25  2012-01-08 2012-01-05     981

Groupby和sum

df_all.groupby(['(datetimeindex)', 'Product', 'Price'], as_index=False)['Weight'].sum()

 (datetimeindex)     Product  Price  Weight
0      2012-01-01       Apple   1.25    1079
1      2012-01-01      Banana   0.55     572
2      2012-01-01  Strawberry   2.10     130

答案 1 :(得分:1)

确保您的Date列是日期

Weights.Date = pd.to_datetime(Weights.Date)

还要确保解决香蕉错字。

我们可以使用pd.merge_asof查找小于或等于目标日期的最新日期。

pd.merge_asof(
    Weights, Prices, left_on=['Date'], right_on=['(datetimeindex)'], by='Product'
).groupby(
    ['(datetimeindex)', 'Product']
).agg(dict(Weight='sum', Price='mean')).reset_index()

  (datetimeindex)     Product  Price  Weight
0      2012-01-01       Apple   1.25    1079
1      2012-01-01      Banana   0.55     572
2      2012-01-01  Strawberry   2.10     130