2

I have this a Dataframe with 200.000 rows that looks like this:

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()

Now I want to iterate over the rows of this frame. First off all i want to check if there are other guess with the same ID, in that case, i want identify how many of products the guess have active at the time it buy the product.

The output what i am looking for is:

Output:

   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

I have been trying with "iterrows()" but it is too big for it.

  • please provide a rationale explaining why the rows should be ordered like you show. jfyi: you don't have `ID` in `input df` – Danila Ganchar May 26 '20 at 13:46
  • i have ordered before in order to make the explanation easier. but it could be ordered in other order. – Daniel Sánchez de Bustos May 26 '20 at 13:52
  • I mean details... why expected ID is `1734`, `3470` if guess (`347`, `173`)? Look at counter value in records with `ID` 2-6. how it should works? – Danila Ganchar May 26 '20 at 13:57
  • Sorry, ID is guess. i did it wrong. What i really want is to get the last column (counter). – Daniel Sánchez de Bustos May 26 '20 at 14:10
  • ok... and how should [works](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) `counter`? – Danila Ganchar May 26 '20 at 14:12
  • imagine the product that i have bought is a account bank. And guess number is the identification of each guess, in our case, we have two different people (173 and 347). I want to know how many accounts open they had in each product. So firts of all i have to check if it is the same person, after that i have to check if when the account start there were other account opened. In line 0 and 1, the counter is 0 because when the account start, there are no other account open, account 0 end the 2018-05-05 and account 1 start 2018-07-06 – Daniel Sánchez de Bustos May 26 '20 at 14:29

0 Answers0