1

enter image description here

How do i create a dataframe which first cell has a different pandas formula from the subsequent cells?

From the above, The first cell of the Ave Gain is simply the average of the first 2 gains: (1.84 + 0) / 2 = 0.92. Similarly for first cell of the Ave Loss : (0.07 + 0) / 2 = 0.035

However the second cell of the Ave Gain is the previous Ave Gain (0.92) + the new Gain (1.62) divided by 2 : (0.92+1.62) /2 = 1.27

and the third cell is the previous Ave Gain (1.27) + the new gain (1.61) divided by 2: = (1.27+1.61) / 2 = 1.44

1)How should i go about creating a dataframe which first cell has a different formula from the subsequent cells?

2)How do i create a recursive table for the Ave Gain (it uses its own previous value to calculate the next value)

Thank you

Ganesh
  • 19
  • 5

1 Answers1

0

It seems that original way did not work properly, so I have updated this answer. And the way it works is actually much simpler and more logical than the first one.

CODE:

import pandas as pd


x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a=pd.DataFrame(x)
a['Avg'] = [0 for m in x]
a['Avg'] = [sum(a[:i+1][0])/(i+1) for i, m in enumerate(a[0])]

print(a)

OUTPUT:

    0  Avg
0   1  1.0
1   2  1.5
2   3  2.0
3   4  2.5
4   5  3.0
5   6  3.5
6   7  4.0
7   8  4.5
8   9  5.0
9  10  5.5

As it is not working the same way as loop, you can not reference value in the previous row while you are calculation value for current row, it just does not exist. So you need to base your calculation completely on the data that you have. With enumeration you can refer to index of an element in the list-comprehension, which allows to refer to sub-list of :i+1 elements at any moment and do calculations on that list to simulate value of a previous row.

Some answers for following Questions used to achieve this:

Adding calculated column(s) to a dataframe in pandas

Getting index of item while processing a list using map in python

GSazheniuk
  • 1,340
  • 10
  • 16
  • 1
    I tried doing the above and got this KeyError: 'Avg' . Using my own formulae: a['ave'] = [(a['ave'][i-1] + i)/ 2 if i > 0 else a[0][0] for i,m in enumerate (a[0])] i got the same error as well – Ganesh Feb 02 '18 at 07:04
  • My bad, the whole idea was wrong, I have updated my answer, let me know if you have any questions around it. – GSazheniuk Feb 02 '18 at 16:52