Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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 3.x 在Pandas中的数据帧中迭代行_Python 3.x_Pandas_Loops - Fatal编程技术网

Python 3.x 在Pandas中的数据帧中迭代行

Python 3.x 在Pandas中的数据帧中迭代行,python-3.x,pandas,loops,Python 3.x,Pandas,Loops,我有一个数据框,有200000行,如下所示: import pandas as pd inp = [{'guess':173, 'start_date':'2004-05-06', 'end_date':'2004-05-06'}, {'guess':173, 'start_date':'2018-07-06', 'end_date':'2018-07-05'}, {'guess':347, 'start_date':'2011-05-30', 'end_date':'2018-10

我有一个数据框,有200000行,如下所示:

import pandas as pd
inp = [{'guess':173, 'start_date':'2004-05-06', 'end_date':'2004-05-06'}, {'guess':173, 'start_date':'2018-07-06', 'end_date':'2018-07-05'},
      {'guess':347, 'start_date':'2011-05-30', 'end_date':'2018-10-09'}, {'guess':347, 'start_date':'2011-10-27 ', 'end_date':'2099-01-01'},
       {'guess':347, 'start_date':'2015-12-29', 'end_date':'2099-01-01'},{'guess':347, 'start_date':'2016-01-05', 'end_date':'2099-01-01'},
      {'guess':347, 'start_date':'2018-11-02', 'end_date':'2099-01-01'}]
df = pd.DataFrame(inp)
df.head()
现在我想迭代这个框架的行。首先,我想检查是否有其他具有相同ID的猜测,在这种情况下,我想确定猜测在购买产品时有多少产品处于活动状态

我想要的输出是:

输出:

   Guess    start_date    end_date   Counter
0   1734    2004-05-06   2018-05-05     0     
1   1734    2018-07-06   2099-01-01     0   it is 0 because when he buy the 2 item, the first is deleted
2   3470    2011-05-30   2018-10-09     0   
3   3470    2011-10-27   2099-01-01     1
4   3470    2015-12-29   2099-01-01     2   
5   3470    2016-01-05   2099-01-01     3   
6   3470    2018-11-02   2099-01-01     3   it happend the same in line 1

我一直在尝试使用“iterrows()”,但它太大了。

请提供一个基本原理,解释为什么应该像您所展示的那样对行进行排序。jfyi:在
input df
中,您没有
ID
,为了便于解释,我之前已经订购了。但可以按其他顺序订购。我是说细节。。。如果猜测(
347
173
),为什么预期ID是
1734
3470
?查看ID为
2-6的记录中的计数器值。它应该如何工作?对不起,我只是猜测。我做错了。我真正想要的是得到最后一列(计数器)。。。那么
计数器应该如何计数呢?