/**/

Python 减go 2 列并在新列上显示当前值

NamesAccount_1Account_2ID_MovementLess_1Less_2
Peter3570Movement_105
Peter3570Movement_260
Peter3570Movement_310
Peter3570Movement_402
Jhon5560Movement_560
Jhon5560Movement_602
Jhon5560Movement_703
Jhon5560Movement_8120
Jhon5560Movement_960
William3488Movement_1008
William3488Movement_1109
William3488Movement_1205

`s = (df['Account_1']).sub(df['Less_1']).groupby(df['Names']).cumsum()`. `df2['New_Account1'] = s`.

NamesAccount 1Account 2ID_MovementLess_1Less_2New_Account1New_Account2
Peter3570Movement_10113559
Peter3570Movement_2602959
Peter3570Movement_3602359
Peter3570Movement_4042355
Jhon5560Movement_5604960
Jhon5560Movement_60144946
Jhon5560Movement_70134933
Jhon5560Movement_81203733
Jhon5560Movement_9603133
William3488Movement_101202288
William3488Movement_11092279
William3488Movement_12052274

推荐答案

``````df[['New_Account1', 'New_Account2']] = (df[['Account_1', 'Account_2']]
- df.groupby('Names')[['Less_1', 'Less_2']]
.cumsum().to_numpy()
)
``````

``````      Names  Account_1  Account_2  ID_Movement  Less_1  Less_2  New_Account1  New_Account2
0     Peter         35         70   Movement_1       0       5            35            65
1     Peter         35         70   Movement_2       6       0            29            65
2     Peter         35         70   Movement_3       1       0            28            65
3     Peter         35         70   Movement_4       0       2            28            63
4      Jhon         55         60   Movement_5       6       0            49            60
5      Jhon         55         60   Movement_6       0       2            49            58
6      Jhon         55         60   Movement_7       0       3            49            55
7      Jhon         55         60   Movement_8      12       0            37            55
8      Jhon         55         60   Movement_9       6       0            31            55
9   William         34         88  Movement_10       0       8            34            80
10  William         34         88  Movement_11       0       9            34            71
11  William         34         88  Movement_12       0       5            34            66
``````