Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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,我正在使用以下数据帧: Price Price2 Count perc_change 0 0.000868 33782.17 4 1.000000 1 0.000872 33224.89 3 0.460829 2 0.000875 84110.85 7 0.344037 3 0.000878 67686.15 4 0.342857 4 0.000880 121400

我正在使用以下
数据帧

      Price     Price2    Count  perc_change
0  0.000868    33782.17     4     1.000000
1  0.000872    33224.89     3     0.460829
2  0.000875    84110.85     7     0.344037
3  0.000878    67686.15     4     0.342857
4  0.000880    121400.22    4     0.227790
以下代码:

for row in df.iterrows():
    print(row)
在df
Dataframe
中一次返回一行,我想知道是否可以一次迭代两行

是,如果可能,您可以使用整数除法返回2行
DataFrame
s:

#default RangeIndex
for i, g in df.groupby(df.index // 2):
    print (g)
使用
numpy.arange
的一般解决方案:

for i, g in df.groupby(np.arange(len(df)) // 2):
    print (g)
      Price    Price2  Count  perc_change
0  0.000868  33782.17      4     1.000000
1  0.000872  33224.89      3     0.460829
      Price    Price2  Count  perc_change
2  0.000875  84110.85      7     0.344037
3  0.000878  67686.15      4     0.342857
     Price     Price2  Count  perc_change
4  0.00088  121400.22      4      0.22779

iterrows
generator对象
,因此您只需在其上调用
next
两次或使用
zip

t = df.iterrows()
for (i, row1), (j, row2) in zip(t, t):
    print(row1, row2)

简而言之,答案是肯定的。您可以看看
shift
。细节如此之少,很难再提供更多帮助了。@AlexandreB。非常感谢您的评论,我刚刚在我的问题中添加了更多信息。非常感谢您的回复。不幸的是,我得到了以下错误:
ValueError:Grouper和axis的长度必须相同
@Fozoro-您可以测试第二个解决方案吗?很抱歉,这是我的错误,现在可以完美地工作了。非常感谢你