我有一个包含两列和大量行的pandas系列,如:
r =
1-10-2010 3.4
1-11-2010 4.5
1-12-2010 3.7
... ...
我想要做的是删除一周中没有自定义周的日子。所以要删除星期五和星期六,做这样的事情:
r = amazingfunction(r, ('Sun', 'Mon', 'Tue', 'Wed', Thu'))
r =
1-10-2010 3.4
1-11-2010 4.5
1-12-2010 3.7
1-13-2010 3.4
1-14-2010 4.1
1-17-2010 4.5
1-18-2010 3.7
... ...
我该如何解决这个问题?
答案 0 :(得分:2)
您可以使用dt.dayofweek
和isin
来过滤df,这里星期五和星期六分别为import scipy.io as sio
import numpy as np
import matplotlib.pyplot as plt
mat_contents = sio.loadmat('SubA_5chan_3LRF')
info = mat_contents['Info']
labels = mat_contents['LABELS']
eegdata = mat_contents['EEGDATA'] # [channels, sample in time, trial]
tam_janela = len(eegdata[0, :, 0])
fig, (ch1, ch2, ch3, ch4, ch5) = plt.subplots(nrows = 5, ncols = 1, sharex = False, sharey=True)
for trial in range(10):
x = np.linspace(0, (tam_janela + tam_janela*trial), (tam_janela + tam_janela*trial) )
ch1.plot((x[(trial*1024):]), eegdata[0, :, trial])
ch1.set_ylabel('Channel 1 (V)')
ch2.plot((x[(trial*1024):]), eegdata[1, :, trial])
ch2.set_ylabel('Channel 2 (V)')
ch3.plot((x[(trial*1024):]), eegdata[2, :, trial])
ch3.set_ylabel('Channel 3 (V)')
ch4.plot((x[(trial*1024):]), eegdata[3, :, trial])
ch4.set_ylabel('Channel 4 (V)')
ch5.plot((x[(trial*1024):]), eegdata[4, :, trial])
ch5.set_xlabel('Time')
ch5.set_ylabel('Channel 5 (V)')
plt.suptitle('EEG recordings from Subject A with 5 channels')
plt.show()
,我们使用4,5
取消布尔掩码:
~
修改强>
由于您的数据是In [12]:
df = pd.DataFrame({'dates':pd.date_range(dt.datetime(2015,1,1), dt.datetime(2015,2,1))})
df['dayofweek'] = df['dates'].dt.dayofweek
df
Out[12]:
dates dayofweek
0 2015-01-01 3
1 2015-01-02 4
2 2015-01-03 5
3 2015-01-04 6
4 2015-01-05 0
5 2015-01-06 1
6 2015-01-07 2
7 2015-01-08 3
8 2015-01-09 4
9 2015-01-10 5
10 2015-01-11 6
11 2015-01-12 0
12 2015-01-13 1
13 2015-01-14 2
14 2015-01-15 3
15 2015-01-16 4
16 2015-01-17 5
17 2015-01-18 6
18 2015-01-19 0
19 2015-01-20 1
20 2015-01-21 2
21 2015-01-22 3
22 2015-01-23 4
23 2015-01-24 5
24 2015-01-25 6
25 2015-01-26 0
26 2015-01-27 1
27 2015-01-28 2
28 2015-01-29 3
29 2015-01-30 4
30 2015-01-31 5
31 2015-02-01 6
In [13]:
df[~df['dates'].dt.dayofweek.isin([4,5])]
Out[13]:
dates dayofweek
0 2015-01-01 3
3 2015-01-04 6
4 2015-01-05 0
5 2015-01-06 1
6 2015-01-07 2
7 2015-01-08 3
10 2015-01-11 6
11 2015-01-12 0
12 2015-01-13 1
13 2015-01-14 2
14 2015-01-15 3
17 2015-01-18 6
18 2015-01-19 0
19 2015-01-20 1
20 2015-01-21 2
21 2015-01-22 3
24 2015-01-25 6
25 2015-01-26 0
26 2015-01-27 1
27 2015-01-28 2
28 2015-01-29 3
31 2015-02-01 6
,因此您的日期是您的索引,因此以下内容应该有效:
Series