使用Pandas时,我可以使用.shift()检查前后行以查看是否满足某些条件。
如果我需要查看是否符合条件,在我与之比较的行之后可能是100行或更多,该怎么办?我知道在数据帧中循环并不是最有效的,但是有人可以请帮助以下示例。
当df [' reversal']列有一个信号时 - 根据下面的例子在14:00显示6.0 - 我想知道在14行之后首先看到以下哪一个:00:
df.price
,其值为df.reversal
+ 1(我们可以看到它在20:00被点击为6 + 1 = 7)df.price
的值为df.reversal
- df.reversal
(在此示例中未点击,因为价格未达到6 - 6 = 0)这是所需的输出,蓝色的单元格显示了我想要看到的内容。新列应显示目标被击中的时间(根据此示例)或目标未命中(如果此示例中的价格达到0)。
请参阅下面的df.to_dict()
重播:
{'move_start': {datetime.time(9, 0): nan,
datetime.time(10, 0): nan,
datetime.time(11, 0): nan,
datetime.time(12, 0): nan,
datetime.time(13, 0): nan,
datetime.time(14, 0): datetime.time(9, 0),
datetime.time(15, 0): nan,
datetime.time(16, 0): nan,
datetime.time(17, 0): nan,
datetime.time(18, 0): nan,
datetime.time(19, 0): nan,
datetime.time(20, 0): nan},
'price': {datetime.time(9, 0): 1,
datetime.time(10, 0): 2,
datetime.time(11, 0): 3,
datetime.time(12, 0): 4,
datetime.time(13, 0): 5,
datetime.time(14, 0): 6,
datetime.time(15, 0): 5,
datetime.time(16, 0): 4,
datetime.time(17, 0): 3,
datetime.time(18, 0): 2,
datetime.time(19, 0): 4,
datetime.time(20, 0): 7},
'reversal': {datetime.time(9, 0): nan,
datetime.time(10, 0): nan,
datetime.time(11, 0): nan,
datetime.time(12, 0): nan,
datetime.time(13, 0): nan,
datetime.time(14, 0): 6.0,
datetime.time(15, 0): nan,
datetime.time(16, 0): nan,
datetime.time(17, 0): nan,
datetime.time(18, 0): nan,
datetime.time(19, 0): nan,
datetime.time(20, 0): nan}}
答案 0 :(得分:1)
让它变得更好更清洁,想到......
import datetime,numpy as np,pandas as pd
nan = np.nan
a = pd.DataFrame({'move_start': {datetime.time(9, 0): nan, datetime.time(10, 0): nan, datetime.time(11, 0): nan, datetime.time(12, 0): nan, datetime.time(13, 0): nan, datetime.time(14, 0): datetime.time(9, 0), datetime.time(15, 0): nan, datetime.time(16, 0): nan, datetime.time(17, 0): nan, datetime.time(18, 0): nan, datetime.time(19, 0): nan, datetime.time(20, 0): nan}, 'price': {datetime.time(9, 0): 1, datetime.time(10, 0): 0, datetime.time(11, 0): 3, datetime.time(12, 0): 4, datetime.time(13, 0): 7, datetime.time(14, 0): 6, datetime.time(15, 0): 5, datetime.time(16, 0): 4, datetime.time(17, 0): 0, datetime.time(18, 0): 2, datetime.time(19, 0): 4, datetime.time(20, 0): 7}, 'reversal': {datetime.time(9, 0): nan, datetime.time(10, 0): nan, datetime.time(11, 0): nan, datetime.time(12, 0): nan, datetime.time(13, 0): nan,
datetime.time(14, 0): 6.0, datetime.time(15, 0): nan, datetime.time(16, 0): nan, datetime.time(17, 0): nan, datetime.time(18, 0): nan, datetime.time(19, 0): nan, datetime.time(20, 0): nan}})
a['target_hit']=nan;
a['target_miss']=nan;
a['reversal1']=a['reversal']+1;
a['reversal2']=a['reversal']-a['reversal'];
a.sort_index(1,inplace=True);
hit = a.ix[:,:-2].dropna()
takeBoth = False
targetIsHit,targetIsMiss = False,False
if takeBoth:
targetHit = a[(hit['reversal1'].values==a['price'].values) & (hit['reversal1'].index.values<a['price'].index.values)];
targetMiss = a[(hit['reversal2'].values==a['price'].values) & (hit['reversal2'].index.values<a['price'].index.values)];
targetIsHit,targetIsMiss = not targetHit.empty, not targetMiss.empty
else:
targetHit = a[(hit['reversal1'].values==a['price'].values) & (hit['reversal1'].index.values<a['price'].index.values)];
targetIsHit = not targetHit.empty
if not targetIsHit:
targetMiss = a[(hit['reversal2'].values==a['price'].values) & (hit['reversal2'].index.values<a['price'].index.values)];
targetIsMiss = not targetMiss.empty
if targetIsHit:a.loc[hit.index.values,"target_hit"] = targetHit.index.values;
if targetIsMiss:a.loc[hit.index.values,"target_miss"] = targetMiss.index.values;
print '#'*50
print a
'''
##################################################
move_start price reversal reversal1 reversal2 target_hit \
09:00:00 NaN 1 NaN NaN NaN NaN
10:00:00 NaN 0 NaN NaN NaN NaN
11:00:00 NaN 3 NaN NaN NaN NaN
12:00:00 NaN 4 NaN NaN NaN NaN
13:00:00 NaN 7 NaN NaN NaN NaN
14:00:00 09:00:00 6 6.0 7.0 0.0 20:00:00
15:00:00 NaN 5 NaN NaN NaN NaN
16:00:00 NaN 4 NaN NaN NaN NaN
17:00:00 NaN 0 NaN NaN NaN NaN
18:00:00 NaN 2 NaN NaN NaN NaN
19:00:00 NaN 4 NaN NaN NaN NaN
20:00:00 NaN 7 NaN NaN NaN NaN
target_miss
09:00:00 NaN
10:00:00 NaN
11:00:00 NaN
12:00:00 NaN
13:00:00 NaN
14:00:00 NaN
15:00:00 NaN
16:00:00 NaN
17:00:00 NaN
18:00:00 NaN
19:00:00 NaN
20:00:00 NaN
'''
PS:作为典型的pythonist我很懒,喜欢处理包含大多数典型问题的普通输入/输出,所以你从开始做出令人讨厌的df,不仅仅是写XD ...关于阅读文本......人们倾向于写,写和写。通常是它的简单问题,但是hella wroten,所以我不理解yota xD。那就像“WTF他想要在这里吗?!”。