This is a bit clumsy, but it gives the correct output, at least for your small example:
import pandas as pd data = {'Employee ID': ["100", "100", "100","100","200","200","200","300"], 'Effective Date': ["2016-01-01","2015-06-05","2014-07-01","2013-01-01","2016-01-01","2015-01-01","2013-01-01","2014-01-01"], 'Desired Output': ["Unique Leave Event 2","Unique Leave Event 2","Unique Leave Event 2","Unique Leave Event 1","Unique Leave Event 2","Unique Leave Event 2","Unique Leave Event 1","Unique Leave Event 1"]} df = pd.DataFrame(data, columns=['Employee ID','Effective Date','Desired Output']) df["Effective Date"] = pd.to_datetime(df["Effective Date"]) df = df.sort_values(["Employee ID","Effective Date"]).reset_index(drop=True) for i,_ in df.iterrows(): df.ix[0,"Result"] = "Unique Leave Event 1" if i < len(df)-1: if df.ix[i+1,"Employee ID"] == df.ix[i,"Employee ID"]: if df.ix[i+1,"Effective Date"] - df.ix[i,"Effective Date"] > pd.Timedelta('365 days'): df.ix[i+1,"Result"] = "Unique Leave Event " + str(int(df.ix[i,"Result"].split()[-1])+1) else: df.ix[i+1,"Result"] = df.ix[i,"Result"] else: df.ix[i+1,"Result"] = "Unique Leave Event 1"
Note that this code assumes that the first line always contains the Unique Leave Event 1 .
EDIT: Some explanation.
First, I convert the dates to a date and time format, and then I reorder the data so that the dates for each employee ID increase.
Then I iterrows over the frame lines using the iterrows built-in iterator. _ in for i,_ is just a placeholder for the second variable, which I do not use, because the iterator returns line and line numbers, I only need numbers.
In the iterator, I do a comparison on a number of lines, so by default I fill in the first line manually, and then assign the i+1 row. I do it this way because I know the value of the first line, but not the value of the last line. Then I compare the i+1 1th line with the i th line inside if -safeguard, because i+1 will give an index error for the last iteration.
In the loop, I first check to see if the Employee ID has changed between the two lines. If this is not the case, I compare the dates of the two lines and see if they are separated for more than 365 days. If so, I read the line "Unique Leave Event X" from line i -th, increased the number by one, and wrote it in i+1 -row. If the dates are closer, I just copy the line from the previous line.
If I change the Employee ID , on the other hand, I just write "Unique Leave Event 1" to get started.
Note 1: iterrows() has no parameters to set, so I can not iterate over only a subset.
Note 2: Always iterate using one of the built-in iterators and only iterate if you cannot solve the problem otherwise.
Note 3: When assigning values ββto iteration, always use ix , loc or iloc .