0

I want to do a calculation where the previous data is percentage added a dataframe like this :

a = 100

with a growth of 10% growth where previously added to be like this :

df = [110,121,133.10,146.41]

1 Answers1

1

Dataframe columns are pandas.Series objects which can be converted into a numpy.narray. Therefore, you can act on the underlying array.

To implement a user-defined accumulative function representing a 10 % increase/growth, we can combine numpy.frompyfunc and numpy.ufunc.accumulate:

import numpy as np

arr = [100, 100, 100, 100, 100]

uadd = np.frompyfunc(lambda x, y: x*1.1, 2, 1)
out = uadd.accumulate(arr, dtype=object).astype(float)[1:]

print(out)

Printing:

[110.   121.   133.1  146.41]

A similar approach was described in this answer and another question which I like to mention as further references.

albert
  • 8,027
  • 10
  • 48
  • 84