【问题标题】:Add column in dataframe from another dataframe matching the id and based on condition in date columns pandas从另一个匹配 id 并基于日期列 pandas 中的条件的数据框中添加数据框中的列
【发布时间】:2022-10-23 14:29:35
【问题描述】:

我的问题是一个非常复杂和令人困惑的问题,我无法在任何地方找到答案。 我基本上有 2 个数据框,一个是某些产品的价格历史记录,另一个是包含交易数据的发票数据框。

样本数据:

价格历史:

    product_id    updated  price
id                              
1            1 2022-01-01    5.0
2            2 2022-01-01    5.5
3            3 2022-01-01    5.7
4            1 2022-01-15    6.0
5            2 2022-01-15    6.5
6            3 2022-01-15    6.7
7            1 2022-02-01    7.0
8            2 2022-02-01    7.5
9            3 2022-02-01    7.7

发票:

   transaction_date  product_id  quantity
id                                       
1        2022-01-02           1         2
2        2022-01-02           2         3
3        2022-01-02           3         4
4        2022-01-14           1         1
5        2022-01-14           2         4
6        2022-01-14           3         2
7        2022-01-15           1         3
8        2022-01-15           2         6
9        2022-01-15           3         5
10       2022-01-16           1         3
11       2022-01-16           2         2
12       2022-01-16           3         3
13       2022-02-05           1         1
14       2022-02-05           2         4
15       2022-02-05           3         7
16       2022-05-10           1         4
17       2022-05-10           2         2
18       2022-05-10           3         1

我希望实现的是在发票数据框中添加价格列,基于:

  1. 产品编号
  2. 比较更新日期和交易日期,以更新日期 <= 该特定记录的交易日期,基本上是在价格更新后找到最接近的日期。 (MAX 日期 <= 交易日期)

    我设法做到了:

    invoice['price'] = invoice['product_id'].map(price_history.set_index('id')['price'])
    

    但现在需要合并日期条件。

    样本数据的预期结果:

    Expected Result

    感谢您提供正确方向的任何指导,谢谢

【问题讨论】:

  • Pandas merge_asof 可能就是你所追求的

标签: python pandas dataframe


【解决方案1】:

merge_asof 是您正在寻找的内容:

pd.merge_asof(
    invoice,
    price_history,
    left_on="transaction_date",
    right_on="updated",
    by="product_id",
)[["transaction_date", "product_id", "quantity", "price"]]

【讨论】:

    【解决方案2】:

    带 arg 方向的 merge_asof

    merged = pd.merge_asof(
        left=invoice,
        right=price_history,
        left_on="transaction_date",
        right_on="updated",
        by="product_id",
        direction="backward",
        suffixes=("", "_y")
    ).drop(columns=["id_y", "updated"]).reset_index(drop=True)
    
    print(merged)
    
    
        id transaction_date  product_id  quantity  price
    0    1       2022-01-02           1         2    5.0
    1    2       2022-01-02           2         3    5.5
    2    3       2022-01-02           3         4    5.7
    3    4       2022-01-14           1         1    5.0
    4    5       2022-01-14           2         4    5.5
    5    6       2022-01-14           3         2    5.7
    6    7       2022-01-15           1         3    6.0
    7    8       2022-01-15           2         6    6.5
    8    9       2022-01-15           3         5    6.7
    9   10       2022-01-16           1         3    6.0
    10  11       2022-01-16           2         2    6.5
    11  12       2022-01-16           3         3    6.7
    12  13       2022-02-05           1         1    7.0
    13  14       2022-02-05           2         4    7.5
    14  15       2022-02-05           3         7    7.7
    15  16       2022-05-10           1         4    7.0
    16  17       2022-05-10           2         2    7.5
    17  18       2022-05-10           3         1    7.7
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 1970-01-01
      • 2022-10-24
      • 2018-08-04
      • 1970-01-01
      相关资源
      最近更新 更多