文件包含日期和时间的数据: 我只想删除包含这些日期之间的行,并包括开始日期和结束日期:
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)
有什么想法吗?
答案 0 :(得分:4)
示例:
flush index
将boolean indexing
用于按条件进行过滤,并按条件按rng = pd.date_range('2017-07-02', periods=10, freq='10D')
df = pd.DataFrame({'Date': rng, 'a': range(10)})
print (df)
Date a
0 2017-07-02 0
1 2017-07-12 1
2 2017-07-22 2
3 2017-08-01 3
4 2017-08-11 4
5 2017-08-21 5
6 2017-08-31 6
7 2017-09-10 7
8 2017-09-20 8
9 2017-09-30 9
进行按位或:
|
或通过Series.between
过滤并通过start_date = '2017-08-01'
end_date = '2017-08-29'
df1 = df[(df['Date'] < start_date) | (df['Date'] > end_date)]
print (df1)
Date a
0 2017-07-02 0
1 2017-07-12 1
2 2017-07-22 2
6 2017-08-31 6
7 2017-09-10 7
8 2017-09-20 8
9 2017-09-30 9
反转掩码:
~