此代码用于查找特定时间范围内的延迟交货(在此示例中为2018年),并将数据写入csv文件(otdedit.csv)。但是,尽管按年正确过滤了数据,但不是迟交的值也不会被过滤掉。我的问题是,如何过滤仅将延迟交货的行写入CSV文件otdedit.csv。
import pandas as pd
from datetime import datetime
from datetime import timedelta
PURCHASE_ORDER = 'Material'
DELIVERY_DATE = 'Delivery Date'
DESIRED_DATE = 'Desired Delivery'
DELAYED_DAYS = 'Delayed Days'
df = pd.read_csv('otd.csv', index_col=PURCHASE_ORDER)
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)
df['Delivery Date'] = pd.to_datetime(df['Delivery Date'], format='%m/%d/%Y')
df['Desired Delivery'] = pd.to_datetime(df['Desired Delivery'], format='%m/%d/%Y')
df2 = df[(df['Delivery Date'].dt.year >= 2018) & (df['Delivery Date'].dt.year <= 2018)]
df2['Diff Deliv Date'] = df2['Delivery Date'] - df2['Desired Delivery']
df2.to_csv('otdedit.csv', sep=',')
这是otdedit.csv的快照,请注意延迟天数为0的行仍会出现。
(此外,我也不知道为什么该程序也没有按标题过滤,我只希望出现这4列,但是原始文件中的每一列都显示了(我已经隐藏了快照)
如果需要,这也是示例数据:
Material Delivery Date Desired Delivery Delayed Days Diff Deliv Date
20030650 1/3/2018 12/22/2017 12 days 00:00:00.000 12 days 00:00:00.00000
20056352 1/2/2018 12/31/2017 2 days 00:00:00.00000 2 days 00:00:00.000000
20052196 10/18/2018 10/18/2018 0 days 00:00:00.0000 0 days 00:00:00.0000000
20031687 1/3/2018 12/27/2017 7 days 00:00:00.0000 7 days 00:00:00.000000
20031687 2/3/2018 2/3/2018 0 days 00:00:00.00000 0 days 00:00:00.000000
20056053 5/14/2018 3/11/2017 429 days 00:00:00.00 429 days 00:00:00.0000000
20070547 1/2/2018 8/15/2017 140 days 00:00:00.0000 140 days 00:00:00.00
答案 0 :(得分:2)
行
java.lang.NoSuchMethodException
正在将视图的副本创建到原始数据框中,并删除给定的列,但是您并未将此副本分配给任何对象。原始数据帧defaultValue
不变。
创建df2后可以执行的操作是:
df[late_deliveries].drop([DELIVERY_DATE, DESIRED_DATE], axis=1)