scott martin
scott martin

Reputation: 1293

Pandas - Find if value exists in a Dataframe based on certain condition

I have a Dataframe that has list of tickets, along wit their sprints and status as below:

ticket,sprint,status
101,sprint_1,Closed
102,sprint_1,Open
103,sprint_2,Working
103,sprint_3,Fixed
103,sprint_4,Fixed
103,sprint_5,Open
103,sprint_6,Closed

I am trying to find tickets that were not Closed in a particular sprint if they is another sprint that is part of.

In the given sample set, we see ticket 102 was not completed in a given sprint but has no future sprints that is part of while ticket 103 moved from sprint_2 to sprint_3 and was closed in sprint_3.

I am trying to add find tickets that were not Closed in a given sprint if they have another entry for a future sprint

Expected output:

ticket,sprint,status,part_of_future_sprint_if_not_closed,no_future_sprint_planned_open_tickets
101,sprint_1,Closed,0,0
102,sprint_1,Open,0,1
103,sprint_2,Working,1,0
103,sprint_3,Fixed,1,0
103,sprint_4,Fixed,1,0
103,sprint_5,Open,1,0
103,sprint_6,Closed,0,0

Upvotes: 1

Views: 34

Answers (1)

jezrael
jezrael

Reputation: 863611

Use:

#test equal
m1 = df['status'].eq('Open')
#test all duplicated tickets
m2 = df['ticket'].duplicated(keep=False)
#test all duplicated sprints
m3 = df['sprint'].duplicated(keep=False)
#test equal
m4 = df['status'].eq('Closed')
#test if at least one Open per group
m5 = m1.groupby(df['ticket']).transform('any')

df['part_of_future_sprint_if_not_closed'] = (m2 & ~m4 & m5).astype(int)
df['no_future_sprint_planned_open_tickets'] = (m1 & ~m2 & m3).astype(int)

print (df)
   ticket    sprint   status  part_of_future_sprint_if_not_closed  \
0     101  sprint_1   Closed                                    0   
1     102  sprint_1     Open                                    0   
2     103  sprint_2  Working                                    1   
3     103  sprint_3    Fixed                                    1   
4     103  sprint_4    Fixed                                    1   
5     103  sprint_5     Open                                    1   
6     103  sprint_6   Closed                                    0   

   no_future_sprint_planned_open_tickets  
0                                      0  
1                                      1  
2                                      0  
3                                      0  
4                                      0  
5                                      0  
6                                      0  

Upvotes: 1

Related Questions