0

I have just started to learn python and I am having the following problem:

for i in range(len(A)):
    A1 = (A['wap'][i]-A['wap'][i+1]) + (A['wap'][i+1])

The problem when the index is in the last element it will try to take the next element that doesn't exist. I am not sure how to fix it.

2
  • 2
    range(len(A)-1) Commented Oct 19, 2017 at 14:06
  • 2
    Hi, I'm not sure exactly what you're trying to do, but in this loop all you're doing is assigning over and over to the variable A1. This doesn't have any effect. Commented Oct 19, 2017 at 14:07

4 Answers 4

2

You can try this. It will take list element from 0 to last-1

for i in range(0,len(A)-1):
    A1 = (A['wap'][i]-A['wap'][i+1]) + (A['wap'][i+1])
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! I actually did this and it work it! But I am not saving in an Array I believe because I tried to access it as a matrix and it doesn work
1

You can do this to limit the range of the for loop:

for i in range(len(A) - 1):
    A1 = (A['wap'][i]-A['wap'][i+1]) + (A['wap'][i+1])

Comments

1

you didn't mentioned , what you would add or subtract while its the last element . if you don't add or subtract anything to the last element than you could easily get away with a if statement

for i in range(len(A)):

if (i<len(A)):
    A1 =  (A[i]-A['wap'][i+1]) + (A['wap'][i+1])
else  
    A=A['wap'][i]

if you want to loop back and add or subtract the very fast element to the last element use :

for i in range(len(A)):

    A1 =  (A['wap'][i]-A['wap'][i%len(A)+1]) + (A['wap'][i%len(A)+1])

this will add and subtract A['wap'][0] to the very last element .

but you should not use for i in range(len(A)-1) because then you are always ignoring the last element of the array .

4 Comments

Yes, this really works but now I realized that I only have one result in the end so I am not creating like a matrix. I tried to do this: A1[i][0] but I got an error
A1[i][0] wont work as your 2D array looks like A1["wap"][i] according to the code you submitted . so the row index is a string here 'wap' . post your array A as well , then I would be able to help you more .
Thanks a lot! I actually my A is something like this: 5 columns composed by index, date from, date until, product and wap and 15 rows of values. I would like that my A1 would be an extension with the new values in a new column like results with the values that I just acquired in a column so just increasing my old matrix. so i wanna extend my original with the new values so they can be easily related.
check out the new comment i added . i guess i gave you what you needed . a quick tips :rather matrix start working dataframe . its much fun and easy
0
import pandas as pd // imports pandas package to create datsfranme  
from IPython.display import display //  import display to display your dataframe 
import numpy as np

create your dataframe with 5 columns and 5 columns

A = pd.DataFrame({"index":[1,2,3,4,5], "date
from":["1/2/2017","2/2/2017","3/2/2017","4/2/2017","5/2/2017"],
              "date until":  
["1/3/2017","2/3/2017","3/3/2017","4/3/2017","5/3/2017"],
 "product":["a","b","c","d","e"] , 
                  "wap":[100,200,300,400,500] })

create the new column you need

 A["wap_update"]=np.nan

display your dataframe

 display(A.head())
 for i in range(len(A)):

    if (i<len(A)-1):

    A.at[i,"wap_update"] =  (A.iloc[i]['wap'] )+ (A.iloc[i+1]['wap'])
else : 

    A.at[i,"wap_update"]=A.iloc[i]['wap']

display(A)

A.iloc[i]['wap'] will get values at i row and "wap" column A.at[i,"wap_update"] will set the value at at i row and "wap" column

1 Comment

Perfect! Thanks a lot

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.