Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.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_Pandas - Fatal编程技术网

Python 在熊猫系列中循环时获取下一个和上一个条目

Python 在熊猫系列中循环时获取下一个和上一个条目,python,pandas,Python,Pandas,在熊猫系列中循环时,如何获得下一个和上一个迭代值?我想计算下一个指数和上一个指数之间的差异,大致如下: data = pd.Series([1,2,4,2,5], index = [100,120,130,140,170]) for idx in data.iteritems(): delta = idx.next()[0] - idx.previous()[0] # do other stuff 我会分别处理第一个和最后一个元素,所以现在让我们忽略它们 这些对你有用吗 In

在熊猫系列中循环时,如何获得下一个和上一个迭代值?我想计算下一个指数和上一个指数之间的差异,大致如下:

data = pd.Series([1,2,4,2,5], index = [100,120,130,140,170])
for idx in data.iteritems():
    delta = idx.next()[0] - idx.previous()[0]
    # do other stuff
我会分别处理第一个和最后一个元素,所以现在让我们忽略它们

这些对你有用吗

In [32]: data = pd.Series([1,2,4,2,5], index = [100,120,130,140,170])

In [33]: data
Out[33]: 
100    1
120    2
130    4
140    2
170    5
dtype: int64

In [34]: data.diff()
Out[34]: 
100   NaN
120     1
130     2
140    -2
170     3
dtype: float64

In [35]: data-data.shift(1)
Out[35]: 
100   NaN
120     1
130     2
140    -2
170     3
dtype: float64

In [36]: data-data.shift(-1)
Out[36]: 
100    -1
120    -2
130     2
140    -3
170   NaN
dtype: float64
这些对你有用吗

In [32]: data = pd.Series([1,2,4,2,5], index = [100,120,130,140,170])

In [33]: data
Out[33]: 
100    1
120    2
130    4
140    2
170    5
dtype: int64

In [34]: data.diff()
Out[34]: 
100   NaN
120     1
130     2
140    -2
170     3
dtype: float64

In [35]: data-data.shift(1)
Out[35]: 
100   NaN
120     1
130     2
140    -2
170     3
dtype: float64

In [36]: data-data.shift(-1)
Out[36]: 
100    -1
120    -2
130     2
140    -3
170   NaN
dtype: float64
这个怎么样

In [55]: for i in range(data.size):
   ....:     print 'now', data[data.index[i]]
   ....:     if i==0: continue
   ....:     if i==data.size-1: continue
   ....:     print '  last: ', data[data.index[i-1]]
   ....:     print '  next: ', data[data.index[i+1]]
   ....:     

now 1
now 2
  last:  1
  next:  4
now 4
  last:  2
  next:  2
now 2
  last:  4
  next:  5
now 5
这个怎么样

In [55]: for i in range(data.size):
   ....:     print 'now', data[data.index[i]]
   ....:     if i==0: continue
   ....:     if i==data.size-1: continue
   ....:     print '  last: ', data[data.index[i-1]]
   ....:     print '  next: ', data[data.index[i+1]]
   ....:     

now 1
now 2
  last:  1
  next:  4
now 4
  last:  2
  next:  2
now 2
  last:  4
  next:  5
now 5

对不起,这是下一个和上一个索引,不是值。同样,这似乎做了当前和下一个值,而不是下一个和上一个…但我明白了,我可以提取索引,移动它们,然后创建另一个系列,从中我将获取增量。对不起,这是下一个和上一个索引,而不是值。同样,这似乎做了当前和下一个值,而不是下一个和上一个…但我明白了,我可以提取索引,移动它们,并创建另一个系列,从中我将取delta。仅供参考,我总是尝试矢量化,大多数panda(和numpy)函数将同时在整个结构上运行,这样速度更快。尽管有时需要以不同的方式思考如何处理问题。仅供参考,我始终尝试矢量化,但大多数panda(和numpy)函数将同时在整个结构上运行,这样的速度要快得多。虽然有时需要以不同的方式思考如何处理问题,但这就是我最终要做的。不过,我使用的是data.index[I]本身,而不是data.index[I]。但这是正确的想法,谢谢。这就是我最后要做的。不过,我使用的是data.index[I]本身,而不是data.index[I]。但这是正确的想法,谢谢。