Pandas: What does the compound method is useful for?

Pandas: What does the compound method is useful for?



I found pandas compound dataframe method documentation but it is really terse. I've tried to run it on a simple dataframe, but didn't understand its utility. Here is my example:


In [5]: df = pd.DataFrame(np.arange(6).reshape(3,2), columns=['a', 'b'])

In [6]: df
Out[6]:
a b
0 0 1
1 2 3
2 4 5

In [7]: df.compound()
Out[7]:
a 14
b 47
dtype: int32



What should I use this method for?






Column a means you earn 0% the first year, 200% the second year and 400% the third, for a total of 15x the original amount, or an increase of 1400%. Time units are made up.

– Mad Physicist
Sep 10 '18 at 4:02



a






Its the same as df.a.add(1).cumprod().sub(1) . You can see the compounding evolving, and .compound() yields just the last number

– RafaelC
Sep 10 '18 at 4:04


df.a.add(1).cumprod().sub(1)


.compound()




1 Answer
1



This is more like loan calculation -- compound interest



enter image description here


df.compound()
Out[143]:
a 14
b 47
dtype: int32



So for columns a


a



enter image description here






Ops, I could have just ctrl-click to see the source: (1 + self).prod(axis=axis, skipna=skipna, level=level) - 1

– neves
Sep 11 '18 at 13:39


(1 + self).prod(axis=axis, skipna=skipna, level=level) - 1



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.