<p>这里有一个使用<a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.merge_asof.html" rel="nofollow noreferrer">merge_asof</a>和<code>allow_exact_matches=False</code>的两步解决方案,这样每个商品行与第一个日期严格大于(不等于)商品行日期的价格匹配。你知道吗</p>
<p><code>.agg(sum)</code>使用这样一个事实:添加两个元组将它们组合成一个元组。你知道吗</p>
<p>假设您的数据帧名为<code>df</code>和<code>df2</code>:</p>
<pre><code># Test data adapted from your examples.
# Sorry that this is difficult to copy-paste into pandas
df
Articles
2010-01-04 (though, reliant, advertis, revenu, internet)
2010-01-05 ((googl, expect, nexus), (one, rival, iphon))
2010-01-06 ((while, googl, introduc), (first,), (piec, hardwar))
2010-01-07 ((googl, form), (energi, subsidiari), (appli,))
2010-01-08 ((david, pogu, review), (googl, new, offer))
2010-01-12 ((the, compani), (agre, hand, list), (book, scan))
df2
Price
2010-01-08 602.020
2010-01-15 580.000
2010-01-22 550.010
2010-01-29 529.944
# Solution
price2articles = (pd.merge_asof(df,
df2,
left_index=True,
right_index=True,
allow_exact_matches=False,
direction='forward')
.groupby('Price')
.agg(sum))
result = pd.merge(df2, price2article, left_on='Price', right_index=True)
# To see full contents of wide data, set
# pd.options.display.max_colwidth = 150 or higher (-1 for no limit)
result
Articles
2010-01-08 (though, reliant, advertis, revenu, internet, (googl, expect, nexus), (one, rival, iphon), (while, googl, introduc), (first,), (piec, hardwar), (googl, form), (energi, subsidiari), (appli,))
2010-01-15 ((david, pogu, review), (googl, new, offer), (the, compani), (agre, hand, list), (book, scan))
</code></pre>