1 回答
TA贡献1785条经验 获得超8个赞
样本:
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
用于boolean indexing按条件过滤,|按位或链按:
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
或过滤Series.between和反转掩码~:
df1 = df[~df['Date'].between(start_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
添加回答
举报