我对通过查找对数据帧执行连接感到头痛:
df1
Name ID1 StartDate1 EndDate1 StartDate2 EndDate2
ab 111 1/1/2017 3/1/2017 2/1/2017 4/1/2017
bc 112 2/1/2017 3/1/2017 2/1/2017 4/1/2017
cd 113 1/1/2017 2/1/2017 2/1/2017 4/1/2017
df 114 2/1/2017 4/1/2017 2/1/2017 4/1/2017
fe 115 4/1/2017 5/1/2017 2/1/2017 4/1/2017
df2
ID1 ID2 Date Id1_num id2_num
111 10 1/1/2017 10 11
111 10 2/1/2017 10
111 10 3/1/2017 13 13
111 10 4/1/2017 15 13
112 20 2/1/2017 11 19
112 20 3/1/2017 12
112 20 4/1/2017 19
113 20 1/1/2017 17 5
113 20 2/1/2017 17 14
114 30 2/1/2017 11 13
114 30 3/1/2017 10
114 30 4/1/2017 18 13
115 30 4/1/2017 7 5
115 30 5/1/2017 13 19
我想要我的结果df3
Name ID1 ID2 StartDate1 EndDate1 StartDate2 EndDate2 ID1Date1Count ID1Date1Average ID1Date2Count ID1Date2Average ID2Date1Count ID2Date1Average ID2Date2Count ID2Date2Average
ab 111 10 1/1/2017 3/1/2017 2/1/2017 4/1/2017 2 11.5 2 14 3 11.33 3 12
bc 112 20 2/1/2017 3/1/2017 2/1/2017 4/1/2017 2 11.5 2 11.5 1 19 2 19
cd 113 20 1/1/2017 2/1/2017 2/1/2017 4/1/2017 2 17 1 17 1 14 1 14
df 114 30 2/1/2017 4/1/2017 2/1/2017 4/1/2017 3 13 3 13 2 13 2 13
fe 115 30 4/1/2017 5/1/2017 2/1/2017 4/1/2017 1 19 0 0 1 19 0 0
在哪里
ID1Date1Count
是从日期间隔[StartDate1, EndDate1]
开始的条目计数,
ID1Date2Count
是从日期间隔[StartDate2, EndDate2]
开始的条目计数
ID2Date1Count
是从日期间隔[StartDate1, EndDate1]
开始的条目计数
ID2Date2Count
是从日期间隔[StartDate2, EndDate2]
开始的条目计数
以及他们的平均水平。
这里的额外条件是,如果num < 10
,则不包括num
。你知道吗
非常感谢!你知道吗
不是100%清楚你真正想要什么(例如什么是“num<;10”,哪个num?Id1\u num还是id2\u num?)但我相信下面的内容会让你更接近。你知道吗
第一组df2并将其格式化为日期间隔:
然后将其与df1合并:
同样,输出与所需的输出并不完全匹配,但非常接近。你知道吗
我分解了步骤,在我们得到
dd1
之后,我们可以merge
它回到你的df1
,你可以用fillna(0)
替换NaN
更新
相关问题 更多 >
编程相关推荐