Python newbie here with a challenge I'm working to solve...
My goal is to iterate through a data frame and return what changed line by line. Here's what I have so far:
pseudo code (may not be correct method) step 1: set row 0 to an initial value step 2: compare row 1 to row 0, add changes to a list and record row number step 3: set current row to new initial step 4: compare row 2 to row 1, add changes to a list and record row number step 5: iterate through all rows step 6: return a table with changes and row index where change occurred
d = {
'col1' : [1, 1, 2, 2, 3],
'col2' : [1, 2, 2, 2, 2],
'col3' : [1, 1, 2, 2, 2]
}
df = pd.DataFrame(data=d)
def delta():
changes = []
initial = df.loc[0]
for row in df:
if row[i] != initial:
changes.append[i]
delta()
changes I expect to see: index 1: col2 changed from 1 to 2, 2 should be added to changes list index 2: col 1 and col3 changed from 1 to 2, both 2s should be added to changes list index 4: col 1 changed from 2 to 3, 3 should be added to changes list