我期待按日期过滤以下df,期待在索引值中仅按星期三过滤:
begin=2015-05-14
end=2015-05-22
Date
2015-05-14 81.370003 6.11282 39.753 44.950001
2015-05-15 80.419998 6.03380 39.289 44.750000
2015-05-18 80.879997 6.00746 41.249 44.360001
2015-05-19 80.629997 6.10465 41.047 40.980000
2015-05-20 80.550003 6.14370 41.636 42.790001
2015-05-21 80.480003 6.16096 42.137 43.680000
2015-05-22 80.540001 6.13916 42.179 43.490002
继续......
这就是我的尝试:
df1=df[df.index.dayofweek == 2]
然后尝试:
df.index = pd.date_range(begin,end,freq='W')
在两种情况下均未成功
所需的输出是相同的df,仅返回星期三的行。
答案 0 :(得分:5)
你可以先看过滤:
df = df.loc[begin:end]
df1=df[df.index.dayofweek == 2]
print (df1)
a b c d
2015-05-20 80.550003 6.1437 41.636 42.790001
答案 1 :(得分:1)
不是最优雅的解决方案,可能远离pythonic。但它会做到这一点。 (在运行之前将所有数据放在pandas数据帧中)
import datetime
import pandas as pd
import time
import calendar
b=0
for date in a:
x = time.strptime(date, "%Y-%m-%d") #strips date in to its components
year=x.tm_year #get year this is necessary for the way datetime.date works (to my best understanding)
month=x.tm_mon #get month
day=x.tm_mday #get day
dayofweek=datetime.date(year,month,day).weekday() #use above to determine day of the week
if dayofweek is 2:
df.set_value(b, 'col6', True) #create extra column that is True if day of week is Wednesday
b=b+1
df=df.loc[df['col6'] == True] #drop everything in df that is not a Wednesday observation