【问题标题】:I'm trying to get days between the two dates but it's not working我试图在两个日期之间获得几天,但它不起作用
【发布时间】:2020-06-05 20:36:19
【问题描述】:

我试图从两列中获取两个日期之间的天数,然后将值添加到新列中。共有 97013 行。

from datetime import datetime


def days_between(d1, d2):
    d1 = datetime.strptime(str(d1), "%Y-%m-%d")
    d2 = datetime.strptime(str(d2), "%Y-%m-%d")
    return abs((d2-d1).days)

for x, y, z in zip(order_time.loc[:,'date_order'], order_time.loc[:,'date_delivered'],
                  order_time.loc[:, 'diff']):
    z = days_between(x, y)

但是,我在“diff”列的整个列中返回了相同的值。

我该如何解决这个问题?

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    这行得通吗?

    order_time['days_between'] = (
        pd.to_datetime(order_time['date_delivered']) -
        pd.to_datetime(order_time['date_order'])
    ).apply(lambda x: x.days)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-07
      • 2016-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 2018-11-08
      相关资源
      最近更新 更多