Python 根据条件填充列

Python 根据条件填充列,python,pandas,datetime,numpy,Python,Pandas,Datetime,Numpy,我有2个数据帧,df1: ID Created Date Days_Diff L-22 2016-03-24 220 L-25 2016-02-12 200 df2: 我们的目标是重新计算差异天数,因为它不需要精确的值。 因此,我现在做了以下工作: df1['Days_Diff2'] = [int(i.days) for i in (df2['Closed date'] - df1[

我有2个数据帧,df1:

ID       Created Date          Days_Diff
L-22       2016-03-24            220
L-25       2016-02-12            200 
df2:

我们的目标是重新计算差异天数,因为它不需要精确的值。 因此,我现在做了以下工作:

      df1['Days_Diff2'] = [int(i.days) for i in (df2['Closed date'] - df1['Created Date'])
问题在于:有些ID没有截止日期,因此无法进行计算(我想我会选择NaT)。如果可以计算天,我希望天=天,如果不能,那么天保持不变

可能吗?任何诡计或扭曲都可以做到?我这里需要一个职业选手:D

多谢各位

编辑:检查上面的样本,我想要:df1:

    ID       Created Date         Days_Diff          Days_Diff2
    L-22      2016-03-24            220         (2017-01-03 - 2016-03-24)
    L-25      2016-02-12            200                  200

有一种可能的方法,尽管没有更多的代码很难看到。您可以使用try和except来完成,但是您需要将代码进一步分离,以便它依次执行每个ID,即

try:
  #The code that you normally want to run
except:
  #The code that you want to run if it fails - i.e. if you have a NaT, it will then do a different calculation

如果没有更多的代码,我只能说…

您能在两个数据帧上显示一些最小的示例吗?我确实看到了,请检查
try:
  #The code that you normally want to run
except:
  #The code that you want to run if it fails - i.e. if you have a NaT, it will then do a different calculation