我有一个csv文件,日期,时间,价格,mag,信号。 62035行;每天有42次与文件中的每个唯一日期相关联。列表理解循环
对于每个日期,当信号列中出现'S'时,在'S'出现时附加相应的价格。以下是尝试。
from pandas import *
from numpy import *
from io import *
from os import *
from sys import *
DF1 = read_csv('___.csv')
idf=DF1.set_index(['date','time','price'],inplace=True)
sStore=[]
for i in idf.index[i][0]:
sStore.append([idf.index[j][2] for j in idf[j][1] if idf['signal']=='S'])
sStore.head()
Traceback (most recent call last)
<ipython-input-7-8769220929e4> in <module>()
1 sStore=[]
2
----> 3 for time in idf.index[i][0]:
4
5 sStore.append([idf.index[j][2] for j in idf[j][1] if idf['signal']=='S'])
NameError: name 'i' is not defined
我不明白为什么我的索引不在这里不允许的。谢谢。
我也认为这是奇怪的是:
idf.index.levels [0]将显示日期“不解析”,因为它是在该文件中,但出故障了。尽管parse_date = True作为set_index中的参数。
我提起这事,因为我想边的刷卡问题是这样的:基于以下DSM的评论
for i in idf.index.levels[0]:
sStore.append([idf.index[j][2] for j in idf.index.levels[1] if idf['signal']=='S'])
sStore.head()
我的编辑2012年12月30日:
我想用我的想法得到P,如下面我评论的。其中,如果S = B,对于任何给定的日期,我们利用收盘时间差,1620
v=[df["signal"]=="S"]
t=[df["time"]=="1620"]
u=[df["signal"]!="S"]
df["price"][[v and (u and t)]]
也就是说,“给我的价格在1620(即使它不给!”卖信号“,S),以便我可以用”额外的B“来区分 - 对于B> S的特殊情况,这忽略了对称关系(其中S> B),但现在我想理解这个逻辑问题。
在回溯,这个表达式给出:
ValueError: boolean index array should have 1 dimension
注意,以便调用DF [“时间”] 我做ñ ot set_index here。尝试联合运算符|得出:
TypeError: unsupported operand type(s) for |: 'list' and 'list'
在最大院士的做法,
@Max研究员
点是在一天结束的时候,收出位置寻找;所以我们需要在最后收集所有那些已经累积的B,S的“卸货”价格;但并没有互相排斥。 如果我说:
filterFunc1 = lambda row: row["signal"] == "S" and ([row["signal"] != "S"][row["price"]=="1620"])
filterFunc2 =lambda row: ([row["price"]=="1620"][row["signal"] != "S"])
filterFunc=filterFunc1 and filterFunc2
filteredData = itertools.ifilter(filterFunc, reader)
在回溯:
IndexError: list index out of range
变量'i'在您的迭代过程中创建('for i'),但您正在使用'我'确定迭代对象('idf.index [i] [0]')。由于'idf.index [i] [0]'部分将首先被评估,并且由于此时没有'i'的值,所以将会抛出错误。 – RocketDonkey
@phihag如果我想“做算法”限制在那些日子的42次,所有的日子里。我该如何解决这个问题?你看到上面列表理解的其他问题吗?谢谢。 –
不相关,但你真的**不想做所有'''' - 进口。 ''从numpy导入*'将用'numpy'替代内建'any',它不处理genexps,所以'any((对于我在范围(3)中为False)} == True'; 'from os import *'将用'os.open'替换'open',所以大部分'open'调用都会返回'TypeError:需要一个整数';等等。 – DSM