2 回答
TA贡献2065条经验 获得超14个赞
所以在完成这个项目后,我获得了更多的见识。我找到了一个解决方案,但希望有cleaner一个解决方案。但这有效:我们可以连接来自原始数据帧的行,这些行具有ClockIndate.isnull:
df_merge = pd.merge(df1, df2, on=['Company', 'Resource'], how='left')
df_filter = df_merge[df_merge.ClockInDate.between(df_merge.EffectiveFrom, df_merge.EffectiveTo) | df_merge.EffectiveFrom.isnull()]
df_final = pd.concat([df_filter, df1[df1.ClockInDate.isnull()]], sort=True)
print(df_final)
ClockInDate Company EffectiveFrom EffectiveTo Resource
1 2019-02-09 A 2019-01-01 2099-12-31 ResA
3 2019-02-09 A 2019-01-01 2099-12-31 ResB
4 2019-02-09 A NaT NaT ResC
5 2019-02-09 B NaT NaT ResD
7 2019-02-09 B 2019-01-01 2099-12-31 ResE
9 2019-02-09 B 2019-01-01 2099-12-31 ResF
6 NaT B NaT NaT ResG
添加回答
举报