我有两个数据帧,我需要根据日期值是否适合两个其他日期进行合并.基本上我需要执行外连接,其中B.event_date位于A.start_date和A.end_date之间.似乎合并和连接总是假设一个公共列,在这种情况下,我没有.
A B
start_date end_date event_date price
0 2017-03-27 2017-04-20 0 2017-01-20 100
1 2017-01-10 2017-02-01 1 2017-01-27 200
Result
start_date end_date event_date price
0 2017-03-27 2017-04-20
1 2017-01-10 2017-02-01 2017-01-20 100
2 2017-01-10 2017-02-01 2017-01-27 200
Run Code Online (Sandbox Code Playgroud) 我在Pandas的SQL比较文档中没有看到这一点.Pandas中这个SQL的等价物是什么?
select a.var1, a.var2, b.var1, b.var2
from tablea a, tableb b
where a.var1=b.var1
and a.var2=b.var2
and a.var3 <> b.var3
Run Code Online (Sandbox Code Playgroud)
我有合并代码如下:
df = pd.merge(a, b, on=['VAR1','VAR2'], how='inner')
Run Code Online (Sandbox Code Playgroud)
如何合并"不相等"部分?
and a.var3 <> b.var3
Run Code Online (Sandbox Code Playgroud) 我有以下数据帧:
id begcost endcost
100 1 3
200 10 12
Run Code Online (Sandbox Code Playgroud)
我想要:
id newcost
100 1
100 2
100 3
200 10
200 11
200 12
Run Code Online (Sandbox Code Playgroud)
基本上我需要为begcost和endcost列中的每个值创建一个新行.尝试了多个转置选项,但似乎无法达到我需要的TIA.