删除包含特定日期之间数据的行

该文件包含按日期和时间的数据:我想要的只是删除包含这些日期之间并包括开始日期和结束日期的行:


data_file =pd.read_csv(r"MyFile.csv", header = None)


start_date = '01/08/2017'

end_date = '29/8/2017'


my_dataframe = my_dataframe.drop([start_date : end_date])

data_file = data_file.to_csv('summary.csv', index = False, header = False)

任何想法?


慕桂英3389331
浏览 98回答 1
1回答

慕的地10843

样本:rng = pd.date_range('2017-07-02', periods=10, freq='10D')df = pd.DataFrame({'Date': rng, 'a': range(10)})&nbsp;&nbsp;print (df)&nbsp; &nbsp; &nbsp; &nbsp; Date&nbsp; a0 2017-07-02&nbsp; 01 2017-07-12&nbsp; 12 2017-07-22&nbsp; 23 2017-08-01&nbsp; 34 2017-08-11&nbsp; 45 2017-08-21&nbsp; 56 2017-08-31&nbsp; 67 2017-09-10&nbsp; 78 2017-09-20&nbsp; 89 2017-09-30&nbsp; 9用于boolean indexing按条件过滤,|按位或链按:start_date = '2017-08-01'end_date = '2017-08-29'df1 = df[(df['Date'] < start_date) | (df['Date']&nbsp; > end_date)]print (df1)&nbsp; &nbsp; &nbsp; &nbsp; Date&nbsp; a0 2017-07-02&nbsp; 01 2017-07-12&nbsp; 12 2017-07-22&nbsp; 26 2017-08-31&nbsp; 67 2017-09-10&nbsp; 78 2017-09-20&nbsp; 89 2017-09-30&nbsp; 9或过滤Series.between和反转掩码~:df1 = df[~df['Date'].between(start_date ,end_date)]print (df1)&nbsp; &nbsp; &nbsp; &nbsp; Date&nbsp; a0 2017-07-02&nbsp; 01 2017-07-12&nbsp; 12 2017-07-22&nbsp; 26 2017-08-31&nbsp; 67 2017-09-10&nbsp; 78 2017-09-20&nbsp; 89 2017-09-30&nbsp; 9
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python