Cumulative result with specific number in pandas

Question:

This is my DataFrame:

index, value
10, 109
11, 110
12, 111
13, 110
14, 108
15, 106
16, 100

I want to build another column based on multippliing by 0,05 with cumulative result.

index, value, result
10, 109, 109
11, 110, 109 + (0,05 * 1) = 109,05
12, 111, 109 + (0,05 * 2) = 109,1
13, 110, 109 + (0,05 * 3) = 109,15
14, 108, 109 + (0,05 * 4) = 109,2
15, 106, 109 + (0,05 * 5) = 109,25
16, 100, 109 + (0,05 * 6) = 109,3

I tried to experiment with shift and cumsum, but nothing works. Can you give me an advice how to do it?

Now I do something like:

counter = 1
result = {}
speed = 0,05
for item in range (index + 1, last_row_index + 1):
    result[item] = result[first_index] + speed * counter
    counter += 1

P.S. During your answers I’ve edited column result. Please don’t blame me. I am really silly person, but I try to grow.

Thank you all for your answers!

Asked By: Igor K.

||

Answers:

Use numpy:

df['result'] = df['value'].iloc[0]*1.05**np.arange(len(df))

Output:

   index  value      result
0     10    109  109.000000
1     11    110  114.450000
2     12    111  120.172500
3     13    110  126.181125
4     14    108  132.490181
5     15    106  139.114690
6     16    100  146.070425

After you edited the question:

df['result'] = df['value'].iloc[0]+0.05*np.arange(len(df))

output:

   index  value  result
0     10    109  109.00
1     11    110  109.05
2     12    111  109.10
3     13    110  109.15
4     14    108  109.20
5     15    106  109.25
6     16    100  109.30
Answered By: mozway

if indices are consecutive

df['result'] = (df['index'] - df['index'][0]) * 0.05 + df['value'][0]

or not:

df['result'] = df.value.reset_index().index * 0.05 + df.value[0]
Answered By: MoRe
df['result'] = np.arange(len(df)) * 0.05
df['result'] = df['value'].add(df['result'])
print(df)

Output:

   value  result
0    109  109.00
1    110  110.05
2    111  111.10
3    110  110.15
4    108  108.20
5    106  106.25
6    100  100.30
Answered By: BeRT2me
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.