是否可以根据指定的条件从另一个数据框数据填充数据框?
df1 =(条件日期范围)
company start date end date
0 a 2023-01-02 2023-01-06
1 b 2023-01-05 2023-01-12
2 c 2023-01-04 2023-01-13
3 d 2023-01-03 2023-01-10
字符串
df2 =数据
DATE a b c d
0 2023-01-02 1 10 30 40
1 2023-01-03 2 11 31 41
2 2023-01-04 3 12 32 42
3 2023-01-05 4 13 33 43
4 2023-01-06 5 14 34 44
5 2023-01-09 6 15 35 45
6 2023-01-10 7 16 36 46
7 2023-01-11 8 17 37 47
8 2023-01-12 9 18 38 48
9 2023-01-13 10 19 39 49
型
df3 =期望输出
DATE a b c d
0 2023-01-02 1.0 NaN NaN NaN
1 2023-01-03 2.0 NaN NaN 41.0
2 2023-01-04 3.0 NaN 32.0 42.0
3 2023-01-05 4.0 13.0 33.0 43.0
4 2023-01-06 5.0 14.0 34.0 44.0
5 2023-01-09 NaN 15.0 35.0 45.0
6 2023-01-10 NaN 16.0 36.0 46.0
7 2023-01-11 NaN 17.0 37.0 NaN
8 2023-01-12 NaN 18.0 38.0 NaN
9 2023-01-13 NaN NaN 39.0 NaN
型
2条答案
按热度按时间vmpqdwk31#
首先转换为日期时间
字符串
步骤2
melt
& usemerge_asof
& chk range &pivot
型
df3:
型
示例代码
型
guicsvcw2#
感谢@pandaskim提供了一个可行的数据。一个选项是获取df 1匹配df 2的位置;我们已经知道
company
列与df 2(a,b,c,d
)中的列顺序相同,我们利用这一点来创建新的框架,并在pyjanitor的conditional_join和numpy的帮助下:字符串