我有一些表的折扣率取决于代理和它的时间段,我想应用它在另一个表,以获得当前适用的利率对他们的销售日期。
这是比率表(df_r)
Agentname ProductType OldRate NewRate StartDate EndDate
0 VSFAAL SPORTS 0.0 10.0 2020-11-05 2021-01-18
1 VSFAAL APPAREL 0.0 35.0 2020-11-05 2022-05-03
2 VSFAAL SPORTS 10.0 15.0 2021-01-18 2022-05-03
3 VSFAALJS SPORTS 0.0 10.0 2020-11-07 2022-05-03
4 VSFAALJS APPAREL 0.0 15.0 2020-11-07 2021-11-09
5 VSFAALJS APPAREL 15.0 5.0 2021-11-09 2022-05-03
这是事务表(df)
Date Sales Agentname ProductType
0 2020-12-01 08:00:02 100.0 VSFAAL SPORTS
1 2022-03-01 08:00:09 99.0 VSFAAL APPAREL
2 2022-03-01 08:00:14 75.0 VSFAAL SPORTS
3 2021-05-01 08:00:39 67.0 VSFAALJS SPORTS
4 2020-05-01 08:00:51 160.0 VSFAALJS APPAREL
5 2021-05-01 08:00:56 65.0 VSFAALJS APPAREL
我希望得到这样的结果:
Date Sales Agentname ProductType Agentname_rates
0 2020-12-01 08:00:02 100.0 VSFAAL SPORTS 10.0
1 2022-03-01 08:00:09 99.0 VSFAAL APPAREL 35.0
2 2022-03-01 08:00:14 75.0 VSFAAL SPORTS 15.0
3 2021-05-01 08:00:39 67.0 VSFAALJS SPORTS 10.0
4 2020-05-01 08:00:51 160.0 VSFAALJS APPAREL 0
5 2021-05-01 08:00:56 65.0 VSFAALJS APPAREL 15.0
目前我正在做的是循环产品类型,然后代理,然后每个日期的索引
col='Agentname'
for product in list(df.ProductType.unique()):
for uname in list(df[col].unique()):
a = df_r.loc[(df_r['Agentname'] == uname) & (df_r['ProductType'] == product.upper()) &
(df_r['EndDate'] >= df['Date'].min())]
for i in a.index:
df.loc[(df['ProductType'].str.upper() == product.upper()) & (df[col] == uname) & (
df['Date'] >= a.loc[i]['StartDate']) & (df['Date'] <= a.loc[i]['EndDate']),
[f"{col}_rates"]] = a.loc[i]['NewRate']
有没有更有效的方法?
1条答案
按热度按时间bq3bfh9z1#
下面是一种方法
合并product和agentname上的两个DF,然后根据日期进行筛选