Это слишком сложно, чтобы достичь векторизованных методов. Так что IMHO, лучший способ - забыть Pandas и просто обрабатывать массивы. Поскольку в конце мы используем результаты для загрузки фрейма данных, я бы использовал массивы numpy вместо простых списков. Код может быть:
# prepare the numpy arrays for the existing column and the new ones
A = df['a'].to_numpy()
B = np.ndarray(A.shape)
C = np.ndarray(A.shape)
D = np.ndarray(A.shape)
# initialize initial values of b, c and d to 0
b = c = d = 0
# loop over A and compute b, c, and d according to the requirements
for i, a in enumerate(A):
if a + b > 0:
b += a
c = b // 10
d += c
elif a + b + d > 0:
b += a + d
c = b // 10
d = c
else:
b += a
c = d = 0
# feed the arrays
B[i], C[i], D[i] = b, c, d
# add the new columns to the DataFrame
df['B'] = B
df['C'] = C
df['D'] = D
Он дает, как и ожидалось:
day a B C D
0 1 1000 1000.0 100.0 100.0
1 2 0 1000.0 100.0 200.0
2 3 0 1000.0 100.0 300.0
3 4 -1200 100.0 10.0 10.0
4 5 0 100.0 10.0 20.0
5 6 0 100.0 10.0 30.0
6 7 -50 50.0 5.0 35.0
7 8 0 50.0 5.0 40.0
8 9 0 50.0 5.0 45.0
9 10 0 50.0 5.0 50.0
10 11 -100 -50.0 0.0 0.0