此代码用于跟踪哪些交付延迟.我希望找到延迟交货的每个实例,并找到与之关联的采购订单编号.我当前的代码能够生成一个列表,显示指定范围的延迟天数.但是,由于这些值存储在列表中,因此无法找到与后期相关的采购订单编号.我希望在终端打印,采购订单号和交货延迟的天数. (从那里我将使用if语句仅查看值> 0以仅查看哪些交付延迟).我的问题是,我如何在终端上打印,采购订单编号及其旁边的天数(我不知道如何执行此操作,因为所有“迟到天数”的值都存储在列表)
import csv
import pandas as pd
import datetime
def calculate(number):
fileread = pd.read_csv('otd.csv', encoding='latin-1')
Deliveryvalue = fileread['Delivery Date']
Desiredvalue = fileread['source desired delivery date']
date_format = '%m/%d/%Y'
date1 = datetime.datetime.strptime(Deliveryvalue[number], date_format)
date2 = datetime.datetime.strptime(Desiredvalue[number], date_format)
diff= date1 - date2
diff2 = diff.days
return diff2
list = []
for i in range(1,20):
list.append(calculate(i))
for y in list:
if y > 1:
print(list)
打印这个:
[0, 0, 0, 0, 0, 0, 0, 0, -7, 3, 50, 0, 0, 0, 0, 0, 1, -9, 0]
# the negative numbers are early deliveries
这是我的csv文件的一个虚拟示例:
最佳答案
import pandas as pd
# change names appropriately
PURCHASE_ORDER = 'Purchase Order'
DELIVERY_DATE = 'Delivery Date'
DESIRED_DATE = 'Desired Date'
DELAYED_DAYS = 'Delayed Days'
df = pd.read_csv('otd.csv', index_col=PURCHASE_ORDER)
–
>> df
Delivery Date Desired Date
Purchase Order
001 2014-12-31 2014-12-31
002 2014-12-31 2014-12-31
003 2015-01-05 2015-01-05
004 2015-01-05 2015-01-05
005 2015-02-12 2015-02-11
006 2016-02-13 2016-02-11
最后两次送货迟到了.
df[DELIVERY_DATE] = pd.to_datetime(df[DELIVERY_DATE])
df[DESIRED_DATE] = pd.to_datetime(df[DESIRED_DATE])
df[DELAYED_DAYS] = df[DELIVERY_DATE] - df[DESIRED_DATE]
late_threshold = pd.Timedelta(days=0)
late_deliveries = df[DELAYED_DAYS] > late_threshold
–
>> df[late_deliveries].drop([DELIVERY_DATE, DESIRED_DATE], axis=1)
Delayed Days
Purchase Order
005 1 days
006 2 days