Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.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 简单熊猫/小熊猫';索引';矢量化计算_Python_Arrays_Numpy_Pandas - Fatal编程技术网

Python 简单熊猫/小熊猫';索引';矢量化计算

Python 简单熊猫/小熊猫';索引';矢量化计算,python,arrays,numpy,pandas,Python,Arrays,Numpy,Pandas,很抱歉问了一个基本问题。我相信答案很简单,但我已经把头撞在墙上好一阵子想弄明白了。我是python新手,但我理解向量化计算的概念。例如,在以下(非常简单的)代码段中: import pandas as pd ndx = ['a', 'b', 'c', 'd', 'e', 'f'] first = [3, 7, 2, 5, 9, 4] second = [8, 9, 7, 3, 3, 7] first = pd.DataFrame(first, index = ndx) second = pd

很抱歉问了一个基本问题。我相信答案很简单,但我已经把头撞在墙上好一阵子想弄明白了。我是python新手,但我理解向量化计算的概念。例如,在以下(非常简单的)代码段中:

import pandas as pd

ndx = ['a', 'b', 'c', 'd', 'e', 'f']
first = [3, 7, 2, 5, 9, 4]
second = [8, 9, 7, 3, 3, 7]

first = pd.DataFrame(first, index = ndx)
second = pd.DataFrame(second, index = ndx)
我知道first>second将返回一个布尔数组,其中a中的每个元素都大于b中对应的元素,与索引匹配。我知道这种严格的索引匹配是使用熊猫的好处之一,但是

问题:如何在矢量化操作中有效地引用“偏移”索引?例如,如果我想比较b中的下一个值和a中的当前值(第一个['a']>第二个['b'],第一个['b']>第二个['c'],等等),该怎么办?同样,如果我只想在第一个['a']大于第二个['a]和第二个['b']时才返回True,该怎么办

我编写了一些代码,可以通过索引在数组中进行迭代。下面是一个例子:

        if next.at[curr.index[i], 'OI'] > curr.OI[i] and \
        next.at[curr.index[i+1], 'OI'] > curr.OI[i+1] and \
        next.at[curr.index[i], 'Vol'] > curr.Vol[i] and \
        next.at[curr.index[i+1], 'Vol'] > curr.Vol[i+1]:
(next和curr是数据帧。OI和Vol是这些数据帧中的列,我是我的计数器。)我知道这不是pythonic,它也非常慢(这…嗯…可能就是为什么它不是pythonic?哈哈)

先谢谢你

概要:一般问题是如何在熊猫(和numpy)中引用偏移元素

编辑: 感谢Jaime和Tomaugsurger对以下np和pd的回答。知道了。。。有道理

后续问题:如何使用不同长度的数据帧实现pandas移位?假设我有两个重叠的时间序列,一个在前面延伸,另一个在后面延伸。因此,每个索引中都有不匹配的值,并且索引(几乎可以肯定)长度不同。熊猫将不允许shift()出现此错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-35914edbe0ff> in <module>()
----> 1 aa = q['OI'] > r['OI']

C:\Python27\lib\site-packages\pandas\core\ops.pyc in wrapper(self, other)
    540             name = _maybe_match_name(self, other)
    541             if len(self) != len(other):
--> 542                 raise ValueError('Series lengths must match to compare')
    543             return self._constructor(na_op(self.values, other.values),
    544                                      index=self.index, name=name)

ValueError: Series lengths must match to compare
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
---->1 aa=q['OI']>r['OI']
包装器中的C:\Python27\lib\site packages\pandas\core\ops.pyc(self,other)
540名称=\u可能匹配\u名称(自身、其他)
541如果len(self)!=len(其他):
-->542 raise VALUERROR('系列长度必须匹配才能比较')
543返回自构造函数(na_op(self.values,other.values),
544 index=self.index,name=name)
ValueError:序列长度必须匹配才能进行比较

我想我可以做一个步骤,然后使用由索引并集定义的集合,但这似乎是一个低效的额外步骤。(我正在努力学习正确的编码实践,而不仅仅是让我的项目开始工作。)对此有什么想法吗?提前谢谢。

不太清楚熊猫的情况,但在numpy中,您可以通过比较偏移切片来完成这类工作。通过“将下一个值与当前值进行比较”的示例,您可以执行以下操作:

>>> first = np.array([3, 7, 2, 5, 9, 4])
>>> second = np.array([8, 9, 7, 3, 3, 7])
>>> first[:-1] > second[1:]
array([False, False, False,  True,  True], dtype=bool)

切片显然不会将
first
的最后一个元素或
second
的第一个元素与任何东西进行比较。

好的,谢谢。这在np中是有意义的,我明白为什么这是有意义的。熊猫当然不允许这样做,因为索引不匹配,但一定有办法。问题的另一个版本是如何在熊猫中创建差异序列(t-t-1,t-2-t-3,…)。我肯定有一个明显的答案我没有找到。答案是使用
shift
first.shift()>second
<代码>移动移动标签。