pandas add new row based on sum/difference of other rows
问题 df have id measure t1 t2 t3 1 savings 1 2 5 1 income 10 15 14 1 misc 5 5 5 2 savings 3 6 12 2 income 4 20 80 2 misc 1 1 1 df want- add a new row to the measure for each id, called spend, calculated by subtracting measure=income - measure=savings, for each of the periods t1,t2,t3, for each id id measure t1 t2 t3 1 savings 1 2 5 1 income 10 15 14 1 misc 5 5 5 1 spend 9 13 9 2 savings 3 6 12 2 income 4 20 80 2 misc 1 1 1 2 spend 1 14 68 Trying: df.loc[df['Measure'] == 'spend'] = df.loc[df[