【问题标题】:Pandas TimeGrouper & Merge on datetimeindexPandas TimeGrouper & Merge on datetimeindex
【发布时间】: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


    【解决方案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
    

    【讨论】:

    • 如果投反对票的人仍在查看此帖子,我想解决您的疑虑,并可能说服您删除投反对票。如果可以的话,请让我知道这篇文章有什么问题。没用吗?
    【解决方案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
    

    分组和求和

    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
    

    【讨论】:

      猜你喜欢
      • 2017-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多