Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为数据帧中的每个项运行计算,然后创建一个新的计算结果:有什么比for循环更有效的吗?_Python_Python 3.x_Pandas - Fatal编程技术网

Python 为数据帧中的每个项运行计算,然后创建一个新的计算结果:有什么比for循环更有效的吗?

Python 为数据帧中的每个项运行计算,然后创建一个新的计算结果:有什么比for循环更有效的吗?,python,python-3.x,pandas,Python,Python 3.x,Pandas,我已经读到不鼓励使用for循环来构建pandas数据帧,但是我无法找到一种更有效的方法来为我的特定用例进行构建 我有一个数据框stockdata,其中包含股票价格数据(股票行情、日期、开盘、高点、低点、收盘、成交量)。然后我将其切分,只得到最近一个交易日有活动的股票,称为已交易。在那之后,我对交易的每只股票进行了一系列计算,并通过for循环将计算结果附加到名为data的dict列表中。今天,我将列表转换成一个数据帧,然后根据我要查找的每个条件将其分割成更小的数据帧。相关代码如下: traded

我已经读到不鼓励使用for循环来构建pandas数据帧,但是我无法找到一种更有效的方法来为我的特定用例进行构建

我有一个数据框
stockdata
,其中包含股票价格数据(股票行情、日期、开盘、高点、低点、收盘、成交量)。然后我将其切分,只得到最近一个交易日有活动的股票,称为
已交易
。在那之后,我对
交易的每只股票进行了一系列计算,并通过for循环将计算结果附加到名为
data
的dict列表中。今天,我将列表转换成一个数据帧
,然后根据我要查找的每个条件将其分割成更小的数据帧。相关代码如下:

traded = stockdata.loc[stockdata['date'] == stockdata['date'].max()]
cp = compute() #function I wrote
data = []

for stock in traded['ticker']:
    finder = traded.loc[traded['ticker'] == stock] #traded to get only the current stock
    buzz20 = ((finder['volume'] / cp.sma(stock, 'volume', 20).shift(-1)[:1]) - 1) 
    buzz50 = ((finder['volume'] / cp.sma(stock, 'volume', 50).shift(-1)[:1]) - 1)
    #cp.sma(x, y, z) gets pd.DataFrame.rolling.mean(z) of stockdata where rows are 'x' of values in column 'y'
    #buzzxx compares the current volume to the average of the last xx days
    ti = cp.sma(stock, 'close', 7)[:1] / cp.sma(stock, 'close', 65)[:1]
    #rolling mean of 'close' over 7 days / rolling mean over 65 days
    mid, uboll, lboll = cp.boll(stock, 'close')
    #mid is cp.sma(), uboll and lboll are mid + 2 std dev, and mid - 2 std dev, respectively 
    valtraded = (finder['volume'] * finder['vwap'])
    data.append({'ticker': finder['ticker'].item(),
                 'close': finder['close'].item(),
                 'chg': cp.roc(stock, 'close')[:1].item(),
                 'volume': finder['volume'].item(),
                 'valto': valtraded.item(),
                 '20sma': cp.sma(stock, 'close', 20)[:1].item(),
                 '50sma': cp.sma(stock, 'close', 50)[:1].item(),
                 '100sma': cp.sma(stock, 'close', 100)[:1].item(),
                 '4ema': cp.ema(stock, 'close', 4)[:1].item(),
                 '8ema': cp.ema(stock, 'close', 8)[:1].item(),
                 '21ema': cp.ema(stock, 'close', 21)[:1].item(),
                 'uboll': uboll[:1].item(),
                 '20buzz': buzz20.item(), '50buzz': buzz50.item(),
                 'ti': ti.item(), 'volatility': cp.volatility(stock)[:1].item()
                 })

today = pd.DataFrame(data)

不幸的是,运行扫描器平均需要大约一分钟,我认为这太长了,因为
trade
只有大约200行,而pandas通常用于更大的数据集。我很确定for循环效率很低,但我不知道其他方法如何实现

您需要的是关于如何矢量化解决方案的建议。然而,您向我们展示的内容要求我们解读您的意图、所有功能,并找出如何将其矢量化。其他人很可能会试图回答这个问题,我只是告诉你我为什么要把它传下去。如果您向我们提供最少数量的数据并解释您正在尝试的计算,然后向我们展示您所做的尝试,那么您获得问题答案的机会将最大化。现在的感觉就像你要求我们做你的工作一样。@Pirsquare抱歉,如果我的问题是这样的,那不是我的本意。英语实际上是我的第三语言,所以有时我很难把我的想法用语言表达出来,所以我的意图可能在翻译中丢失了。如果你(或任何人)能告诉我这个问题缺少什么,我很乐意编辑它。另外,我会编辑问题并给出解释。我理解。但我希望你能得到一些反馈,而不是让你的问题没有答案,你想知道为什么。这样,您可能仍然会思考如何简化问题。询问如何将一个计算矢量化。解释计算是什么。把它分成几个问题。相信我,我们喜欢提问;-)