The file contains data by date and time: All I want I want drop rows that contains between these dates and includes the start and end dates:
JavaScript
x
8
1
data_file =pd.read_csv(r"MyFile.csv", header = None)
2
3
start_date = '01/08/2017'
4
end_date = '29/8/2017'
5
6
my_dataframe = my_dataframe.drop([start_date : end_date])
7
data_file = data_file.to_csv('summary.csv', index = False, header = False)
8
Any Idea?
Advertisement
Answer
Sample:
JavaScript
1
15
15
1
rng = pd.date_range('2017-07-02', periods=10, freq='10D')
2
df = pd.DataFrame({'Date': rng, 'a': range(10)})
3
print (df)
4
Date a
5
0 2017-07-02 0
6
1 2017-07-12 1
7
2 2017-07-22 2
8
3 2017-08-01 3
9
4 2017-08-11 4
10
5 2017-08-21 5
11
6 2017-08-31 6
12
7 2017-09-10 7
13
8 2017-09-20 8
14
9 2017-09-30 9
15
Use boolean indexing
for filter by condition with chain by |
for bitwise OR:
JavaScript
1
14
14
1
start_date = '2017-08-01'
2
end_date = '2017-08-29'
3
4
df1 = df[(df['Date'] < start_date) | (df['Date'] > end_date)]
5
print (df1)
6
Date a
7
0 2017-07-02 0
8
1 2017-07-12 1
9
2 2017-07-22 2
10
6 2017-08-31 6
11
7 2017-09-10 7
12
8 2017-09-20 8
13
9 2017-09-30 9
14
Or filter by Series.between
and invert mask by ~
:
JavaScript
1
11
11
1
df1 = df[~df['Date'].between(start_date ,end_date)]
2
print (df1)
3
Date a
4
0 2017-07-02 0
5
1 2017-07-12 1
6
2 2017-07-22 2
7
6 2017-08-31 6
8
7 2017-09-10 7
9
8 2017-09-20 8
10
9 2017-09-30 9
11