python 3根据日期列从字符串中删除csv中的行(字符串格式)

时间:2017-06-28 01:50:26

标签: python csv pandas datetime

我试图使用下面的方案来适应我的情景 - 但是失败了

Pandas - Python, deleting rows based on Date column

我有一个带有以下列的

的output.csv文件
Customer, Alertkey, Node, Alertgroup, FirstOccurrence,
TKT_Flag, X733SpecificProb, TKT_TicketNumber, TKT_Keyword

该文件将每隔7天以最近7天的数据递增地从数据库更新

理想情况下,我必须从文件中删除前7天的数据。

我可以写下面但是得到类型错误" TypeError:字符串索引必须是整数"

import pandas as pd
from dateutil.relativedelta import relativedelta
from dateutil import parser


df=pd.read_csv('output.csv', usecols=['FirstOccurrence'],parse_dates=[0])
df=df['FirstOccurrence'].iloc[0]
dt = parser.parse(df)
SevenDays = dt + relativedelta( days = +7 )
df=df[(parser.parse(df['FirstOccurrence']) < SevenDays)].drop(df.columns)

将有数百万行。我从2016年1月1日开始复制前几行。但是从2016年1月1日到现在为止。每周它都会附加并删除前7天的记录 - 即第一次删除记录从1月1日到1月6日等等

Customer,Alertkey,Node,Alertgroup,FirstOccurrence,TKT_Flag,X733SpecificProb,TKT_TicketNumber,TKT_Keyword
Cust1,Cust1_11_53_Services_Warning,Node_Cust1,ITM_K53_SERVICEMON,2016-01-01 00:12:59,1005,TOLPUKC_OS:25223174,INC000014799786,CGMIDDLEWARE_MEDIUM_CONNECTDIRECT
Cust1,Cust1_11_53_Services_Warning,Node1_Cust1,ITM_K53_SERVICEMON,2016-01-01 00:12:59,1005,TOLPUKC_OS:25223175,INC000014799785,CGMIDDLEWARE_MEDIUM_CONNECTDIRECT
Cust2,Cust2_21_NT_System_CPU_Critical,Cust2_Node8,ITM_NT_System,2016-01-01 00:15:48,101,PARPFRC_OS:21192843,INC000000628410,WINDOWS_MEDIUM_DEFPRODUCTSILVER
Cust3,Cust3_10352_LZ_TDW_DISK_Critica,Cust3_Node22,ITM_Linux_Disk,2016-01-01 00:17:05,200,TOLPUKC_OS:25223370,INC000001412280,CGMOM_HIGH_DEFPRODUCT
Cust6,Cust6_11_53_Services_Warning,Cust6_Node700,ITM_K53_SERVICEMON,2016-01-01 00:22:36,22,TOLPUKC_OS:25223601,INC000002250120,CGIOWINTELIMOC_MEDIUM_DEFPRODUCT

1 个答案:

答案 0 :(得分:0)

替换这个: df=df[(parser.parse(df['FirstOccurrence']) < SevenDays)].drop(df.columns)

使用: df = df.drop(df[(parser.parse(df['FirstOccurrence']) < SevenDays)].index, inplace=True)

试试希望这对你有帮助。