pythonpandaspython-deepdiff

Add DeepDiff output back to original df


I apologize if this is a possible duplicate and a trivial question. I am trying to calculate the difference between diff column in my df for consecutive rows.

Here is my code for the same:

z = prac_df.sort_values(['customer_id', 'delivery_date'])

grouped = z.groupby('customer_id')
differences = []
for name, group in grouped:
    group = group.sort_values('delivery_date')
    for i in range(1, len(group)):
        diff = DeepDiff(group.iloc[i-1]['diff'], group.iloc[i]['diff'])
        if diff:
            differences.append(diff)
        else:
            differences.append('no change')

How can I add the differences back to my original df z?


Solution

  • Sorry for my mistaken comment, Anyway you can do the following:

    z['diff'] = 0
    for name, group in grouped:
        d = ['first item']
        group = group.sort_values('delivery_date')
        for i in range(1, len(group)):
            diff = DeepDiff(group.iloc[i-1]['diff'], group.iloc[i]['diff']) 
            if diff:
                d.append(diff)
            else:
                d.append('no change')
        z.loc[group.index, 'diff'] = d
    

    Note that you need a default value for first items