如果在某个范围内,则根据日期合并两个df,并对值取平均值


df_A
start_date  end_date
0   2017-03-01  2017-04-20
1   2017-03-20  2017-04-27
2   2017-04-10  2017-05-25
3   2017-04-17  2017-05-22
df_B
event_date  price
0   2017-03-15  100
1   2017-02-22  200
2   2017-04-30  100
3   2017-05-20  150
4   2017-05-23  150

结果

start_date  end_date        avg.price
0   2017-03-01  2017-04-20      100.0
1   2017-03-20  2017-04-27      
2   2017-04-10  2017-05-25      133.3
3   2017-04-17  2017-05-22      125

如果数据帧不大,一种方法是使用笛卡尔乘积并过滤数据帧。

mapper = df_A.assign(key=1).merge(df_B.assign(key=1))
.query('start_date <= event_date <= end_date')
.groupby('start_date')['price'].mean()
df_A['avg.price'] = df_A['start_date'].map(mapper)
print(df_A)

输出:

start_date   end_date   avg.price
0 2017-03-01 2017-04-20  100.000000
1 2017-03-20 2017-04-27         NaN
2 2017-04-10 2017-05-25  133.333333
3 2017-04-17 2017-05-22  125.000000

否则,请参阅此,以便张贴

pyjanitor的

conditional_join可能有助于抽象/方便;该功能目前正在开发中:

# pip install git+https://github.com/pyjanitor-devs/pyjanitor.git
import pandas as pd
import janitor
(df_B.conditional_join(
df_A, 
('event_date', 'start_date', '>='), 
('event_date', 'end_date', '<='), 
how = 'right')
.droplevel(level = 0, axis = 1)
.loc[:, ['price', 'start_date', 'end_date']]
.groupby(['start_date', 'end_date'])
.agg(avg_price = ('price', 'mean'))
)
avg_price
start_date end_date
2017-03-01 2017-04-20  100.000000
2017-03-20 2017-04-27         NaN
2017-04-10 2017-05-25  133.333333
2017-04-17 2017-05-22  125.000000

在引擎盖下,它使用二进制搜索(np.searchsorted(来避免笛卡尔乘积。如果间隔不重叠,pd.IntervalIndex将是更有效的选择。

最新更新