1

I have a function that use b(t-1) variable like:

def test_b(a,b_1):
     return a + b_1

Assume the following dataframe:

df = pd.DataFrame({'a':[1,2,3],'b':np.nan})

I am assigning the b_1 initial value:

df['b'].ix[0]=0

and then (using my Matlab experience), i use the loop:

for i in range(1,len(df)):
    df['b'].ix[i] = test_b(df['a'].ix[i],df['b'].ix[i-1])

output:

  a|b
0|1|0
1|2|2
2|3|5

Is it a more elegant way to do the same?

3
  • show what you want the output to be Commented Jun 2, 2014 at 14:03
  • jeff, i getting the output i want. I just want to see if another way exist with pandas , like apply or shift. I tried but i couldn't figure out Commented Jun 2, 2014 at 14:08
  • no, what I mean is best to actually show the output in the question Commented Jun 2, 2014 at 14:09

1 Answer 1

1

You never want to do assignments like this, as this is chained indexing

This is a recurrent relation, so not easy way ATM to do this in a super performant manner, though see here.

here is an open issue about this with a pointer to this which uses ifilter to solve the relation.

Sign up to request clarification or add additional context in comments.

3 Comments

for x in df.index[1:]: df.loc[x, 'b'] = df.loc[x, 'a'] + df.loc[x - 1, 'b']..... Is this also bad? I've never quite got to the bottom of this issue...
but I am terrified of it!
that's ok, but would be really slow on a big frame (better to create a list then assign it all at once

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.