使用iterrows时“index.week”过滤

时间:2016-12-02 16:23:24

标签: python pandas

如果当前周尚未与某个值匹配,我想添加一个条件我的基本脚本只查找if row['Val'] > 1.5:。所以我的脚本目前得到了这个(第7周和第8周有两个条目):

Week is: 3.0 
Value is: 2.55241826585
Week is: 6.0 
Value is: 1.55068781498
Week is: 7.0 
Value is: 1.50624949327
Week is: 7.0 
Value is: 1.64751555163
Week is: 8.0 
Value is: 1.79860293902
Week is: 8.0 
Value is: 1.97472511905
Week is: 9.0 
Value is: 2.31025762754
Week is: 10.0 

所需的输出是:

Week is: 3.0 
Value is: 2.55241826585
Week is: 6.0 
Value is: 1.55068781498
Week is: 7.0 
Value is: 1.64751555163
Week is: 8.0 
Value is: 1.79860293902
Week is: 9.0 
Value is: 2.31025762754
Week is: 10.0 

所以第7周和第8周只有一个条目。

伪代码:

如果本周没有其他比赛:    做点什么

数据:

ts = pd.DataFrame(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
ts.columns = ['Val']
ts['Week'] = ts.index.week
for index, row in ts.iterrows():
    if row['Val'] > 1.5:
        print("Week is:",row['Week'],'\n'"Value is:",row['Val'])

0 个答案:

没有答案