Python ItErrors获取下一行值

Python ItErrors获取下一行值,python,pandas,next,Python,Pandas,Next,我对熊猫有兴趣 import pandas as pd df = pd.DataFrame(['AA', 'BB', 'CC'], columns = ['value']) 我想迭代df中的行。对于每一行,我需要行s值和下一行s值 比如(它不起作用): 因此我想要 'AA' 'BB' 'BB' 'CC' 'CC' *Wrong index error here 现在我有办法解决这个问题 for i in range(0, df.shape[0]) print df.irow(i)[

我对熊猫有兴趣

import pandas as pd
df = pd.DataFrame(['AA', 'BB', 'CC'], columns = ['value'])
我想迭代df中的行。对于每一行,我需要行
s值和下一行
s值
比如(它不起作用):

因此我想要

'AA'
'BB'
'BB'
'CC'
'CC'
*Wrong index error here  
现在我有办法解决这个问题

for i in range(0, df.shape[0])
   print df.irow(i)['value']
   print df.irow(i+1)['value']
有没有更有效的方法来解决这个问题

首先,您的“凌乱方式”还可以,在数据帧中使用索引没有问题,而且不会太慢。ItErrors()本身并不是非常快

你的第一个想法的一个版本是:

row_iterator = df.iterrows()
_, last = row_iterator.next()  # take first item from row_iterator
for i, row in row_iterator:
    print(row['value'])
    print(last['value'])
    last = row
第二种方法可以做类似的事情,将一个索引保存到数据帧中:

last = df.irow(0)
for i in range(1, df.shape[0]):
    print(last)
    print(df.irow(i))
    last = df.irow(i)

当速度非常关键时,您始终可以尝试对代码进行计时。

这也可以通过使用自身的偏移版本ping数据帧(迭代器)来解决

当然,索引错误不能以这种方式再现

看看这个

import pandas as pd
from itertools import izip

df = pd.DataFrame(['AA', 'BB', 'CC'], columns = ['value'])   

for id1, id2 in izip(df.iterrows(),df.ix[1:].iterrows()):
    print id1[1]['value']
    print id2[1]['value']

AA
BB
BB
CC
itertools
文档中有一个
pairwise()
函数示例:

from itertools import tee, izip
def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

import pandas as pd
df = pd.DataFrame(['AA', 'BB', 'CC'], columns = ['value'])

for (i1, row1), (i2, row2) in pairwise(df.iterrows()):
    print i1, i2, row1["value"], row2["value"]
以下是输出:

0 1 AA BB
1 2 BB CC
但是,我认为数据帧中的iter行是缓慢的,如果你能解释你想要解决的问题,也许我可以建议一些更好的方法。

我会使用如下shift()函数:

df['value_1'] = df.value.shift(-1)
[print(x) for x in df.T.unstack().dropna(how = 'any').values];
产生

AA
BB
BB
CC
CC
以上代码就是这样工作的:

步骤1)使用换档功能

df['value_1'] = df.value.shift(-1)
print(df)
产生

value value_1
0    AA      BB
1    BB      CC
2    CC     NaN
步骤2)转置:

df = df.T
print(df)
产生:

          0   1    2
value    AA  BB   CC
value_1  BB  CC  NaN
AA
BB
BB
CC
CC
步骤3)取消堆叠:

产生:

0  value       AA
   value_1     BB
1  value       BB
   value_1     CC
2  value       CC
   value_1    NaN
dtype: object
0  value      AA
   value_1    BB
1  value      BB
   value_1    CC
2  value      CC
dtype: object
步骤4)删除NaN值

df = df.dropna(how = 'any')
print(df)
产生:

0  value       AA
   value_1     BB
1  value       BB
   value_1     CC
2  value       CC
   value_1    NaN
dtype: object
0  value      AA
   value_1    BB
1  value      BB
   value_1    CC
2  value      CC
dtype: object
步骤5)返回数据帧的Numpy表示形式,并按值打印值:

df = df.values
[print(x) for x in df];
产生:

          0   1    2
value    AA  BB   CC
value_1  BB  CC  NaN
AA
BB
BB
CC
CC

一组答案让我跑得很快。 使用shift方法创建下一行值的新列, 然后像@alisdt那样使用行迭代器函数, 但在这里,我将它从iterrows更改为itertuples,即100 时间快了

我的脚本用于迭代不同长度的重复数据帧并添加 每次复制一秒钟,因此它们都是唯一的

# create new column with shifted values from the departure time column
df['next_column_value'] = df['column_value'].shift(1)
# create row iterator that can 'save' the next row without running for loop
row_iterator = df.itertuples()
# jump to the next row using the row iterator
last = next(row_iterator)
# because pandas does not support items alteration i need to save it as an object
t = last[your_column_num]
# run and update the time duplications with one more second each
for row in row_iterator:
    if row.column_value == row.next_column_value:
         t = t + add_sec
         df_result.at[row.Index, 'column_name'] = t
    else:
         # here i resetting the 'last' and 't' values
         last = row
         t = last[your_column_num]

希望能有所帮助。

我相信第一个选项的第二行应该是:
\uu,last=row\u iterator.next()
对于Python3,人们可以使用next(row\u iterator)或row\u iterator\uu next\uuuuuuuuuuuuu(),在这个例子中,
\uuuu,
做什么?为什么我不能只做
last=next(row\u迭代器)
,它在这里执行哪一个呢?这非常好。我正在研究一个与原始问题类似的问题,这完美地解决了我的问题。谢谢。在Python3中,您不再需要导入
izip
——内置的
zip
提供了直接链接到Python3
itertools
的功能,非常好!这个很好用,谢谢。这一行
df['value\u 1']=df.value.shift(-1)
是我解决类似需求所需要的全部。非常感谢。