提问人:vp_050 提问时间:12/2/2021 最后编辑:sammywemmyvp_050 更新时间:12/2/2021 访问量:124
在给定另一个数据帧中两列的值约束的情况下,在一个数据帧的列中查找最大值
Find maximum of value in a column of one dataframe given the value constraint of two columns in another dataframe
问:
我有一个数据帧 df1,其中有两列表示任务的开始和结束时间。我还有另一个数据帧,df2,有两列代表时间和当时可用的库存。我想在 df1 中创建另一个名为 max_stock 的列,该列在 df1 的 ST 和 ET 给出的时间范围内具有最大股票值。例如,第一个任务有开始时间和结束时间,因此对于这个值,是 df2 列中值的最大值,在时间 、 和 、 和 时,最大值分别为 10、26 和 48。7/11/2021 1:00
7/11/2021 2:00
max_stock
stock
7/11/2021 1:00
7/11/2021 1:30
7/11/2021 2:00
DF1
ST ET
7/11/2021 1:00 7/11/2021 2:00
7/11/2021 2:00 7/11/2021 3:00
7/11/2021 3:00 7/11/2021 4:00
7/11/2021 4:00 7/11/2021 5:00
7/11/2021 5:00 7/11/2021 6:00
7/11/2021 6:00 7/11/2021 7:00
7/11/2021 7:00 7/11/2021 8:00
7/11/2021 8:00 7/11/2021 9:00
7/11/2021 9:00 7/11/2021 10:00
DF2型
Time stock
7/11/2021 1:00 10
7/11/2021 1:30 26
7/11/2021 2:00 48
7/11/2021 2:30 35
7/11/2021 3:00 32
7/11/2021 3:30 80
7/11/2021 4:00 31
7/11/2021 4:30 81
7/11/2021 5:00 65
7/11/2021 5:30 83
7/11/2021 6:00 40
7/11/2021 6:30 84
7/11/2021 7:00 41
7/11/2021 7:30 15
7/11/2021 8:00 65
7/11/2021 8:30 18
7/11/2021 9:00 80
7/11/2021 9:30 12
7/11/2021 10:00 5
所需 df
ST ET max_stock
7/11/2021 1:00 7/11/2021 2:00 48.00
7/11/2021 2:00 7/11/2021 3:00 48.00
7/11/2021 3:00 7/11/2021 4:00 80.00
7/11/2021 4:00 7/11/2021 5:00 81.00
7/11/2021 5:00 7/11/2021 6:00 83.00
7/11/2021 6:00 7/11/2021 7:00 84.00
7/11/2021 7:00 7/11/2021 8:00 65.00
7/11/2021 8:00 7/11/2021 9:00 80.00
7/11/2021 9:00 7/11/2021 10:00 80.00
答:
2赞
sammywemmy
12/2/2021
#1
一种选择是通过 pyjanitor 的conditional_join来模拟大于和小于条件,然后再进行分组和聚合:
# pip install pyjanitor
import pandas as pd
import janitor
(df1.conditional_join(
df2,
('ST', 'Time', '<='),
('ET', 'Time', '>='))
.groupby(['ST', 'ET'], as_index = False)
.stock
.max()
)
ST ET stock
0 2021-07-11 01:00:00 2021-07-11 02:00:00 48
1 2021-07-11 02:00:00 2021-07-11 03:00:00 48
2 2021-07-11 03:00:00 2021-07-11 04:00:00 80
3 2021-07-11 04:00:00 2021-07-11 05:00:00 81
4 2021-07-11 05:00:00 2021-07-11 06:00:00 83
5 2021-07-11 06:00:00 2021-07-11 07:00:00 84
6 2021-07-11 07:00:00 2021-07-11 08:00:00 65
7 2021-07-11 08:00:00 2021-07-11 09:00:00 80
8 2021-07-11 09:00:00 2021-07-11 10:00:00 80
之后可以使用笛卡尔联接和过滤器(对于大型数据帧,这可能是内存效率低下):
(df1.merge(df2, how='cross')
.query('ST <=Time <= ET')
.groupby(['ST', 'ET'], as_index = False)
.stock
.max()
)
Out[113]:
ST ET stock
0 2021-07-11 01:00:00 2021-07-11 02:00:00 48
1 2021-07-11 02:00:00 2021-07-11 03:00:00 48
2 2021-07-11 03:00:00 2021-07-11 04:00:00 80
3 2021-07-11 04:00:00 2021-07-11 05:00:00 81
4 2021-07-11 05:00:00 2021-07-11 06:00:00 83
5 2021-07-11 06:00:00 2021-07-11 07:00:00 84
6 2021-07-11 07:00:00 2021-07-11 08:00:00 65
7 2021-07-11 08:00:00 2021-07-11 09:00:00 80
8 2021-07-11 09:00:00 2021-07-11 10:00:00 80
另一种选择是使用区间索引(此处的过程较长,因为生成的区间具有重叠值):
box = pd.IntervalIndex.from_arrays(df1.ST, df1.ET, closed='both')
df1.index = box
# create temporary Series
temp = (df2.Time
.apply(lambda x: box[box.get_loc(x)])
.explode(ignore_index = False)
)
temp.name = 'interval'
# lump back to main dataframe (df2)
temp = pd.concat([df2, temp], axis = 1)
# aggregate:
temp = temp.groupby('interval').stock.max()
# join back to df1 to get final output
df1.join(temp).reset_index(drop=True)
ST ET stock
0 2021-07-11 01:00:00 2021-07-11 02:00:00 48
1 2021-07-11 02:00:00 2021-07-11 03:00:00 48
2 2021-07-11 03:00:00 2021-07-11 04:00:00 80
3 2021-07-11 04:00:00 2021-07-11 05:00:00 81
4 2021-07-11 05:00:00 2021-07-11 06:00:00 83
5 2021-07-11 06:00:00 2021-07-11 07:00:00 84
6 2021-07-11 07:00:00 2021-07-11 08:00:00 65
7 2021-07-11 08:00:00 2021-07-11 09:00:00 80
8 2021-07-11 09:00:00 2021-07-11 10:00:00 80
评论
0赞
vp_050
12/2/2021
我们可以在不使用看门人包的情况下实现这一点吗?只能使用 pandas 还是 numpy?
0赞
sammywemmy
12/2/2021
确定;我将添加另一个选项
0赞
vp_050
12/2/2021
第二个代码引发以下错误:MergeError:没有要执行合并的常见列。合并选项:left_on=None、right_on=None、left_index=False、right_index=False
0赞
sammywemmy
12/2/2021
您使用的是哪个版本的 Pandas,因为 Merge With 不需要任何列。我在熊猫 1.3.4how='cross
0赞
vp_050
12/2/2021
版本为 1.1.3。
评论