0

Hi I am trying to create a new array from a previous array. such that in new array the first element is mean of first 20 elements from existing array. Here is my code. I am not sure why its not working.

#Averages
RPMA=[]
for i in range(9580):
  for j in range (0,191600):
    a=RPM.iloc[j:j+20]
    RPMA(i)= a.mean()
1
  • 5
    RPMA(i)= a.mean() => RPMA.append(a.mean()). By the way, note that i plays no role here -- you should check if the first for loop is needed and why Commented Aug 20, 2017 at 3:16

2 Answers 2

1

Looks to me like you're using the wrong kind of brackets. This line:

RPMA(i)= a.mean()

...should probably be this:

RPMA[i]= a.mean()

But I'm no Python expert. I guessing that it thinks RPMA(i) is a function because you use parentheses, in which case you would be trying to assign a value to a function call, like the error says.

However trying to assign a new value past the end of the array will result in an IndexError because the array element doesn't exist, and will need to be added. What you can do instead is this:

RPMA.append(a.mean())

...which will append the data to the end of the array (i.e. adding a new element).

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

5 Comments

This is correct, but this would result in an IndexError when you try assigning out of the range of the list (e.g., xs=[]; xs[0] = 3.14 is always an error. You need to use RPMA.append(a.mean()) to append to the end of the list in this case.
@drjimbob Do you mind if I include that information in my answer? I forgot to consider that you might need to add an extra element to the end of the array, because in JS you don't.
Yeah, no need to ask; this is a collaborative site. Almost wrote this as an answer but someone had this as a comment earlier. Your method would work if the array was the right-size when it was defined with something like RPMA=[None,]*9580.
@drjimbob Coolio, thanks. It is a collaborative site, yes, but it could be considered plagiarism if I just copied your information into my answer without asking. Plus it's just polite, and I've seen people get pretty upset if you don't ask first sometimes :)
@Clonkex I second that. I've seen people get pretty angry because they suddenly deiced they wanted to write they're own answer, based off of a comment they made on your post. I really don't see anything wrong with erring on the side of caution and asking, first.
0

Thanks everyone. As suggested by most of you, I made below changes in code and now its working just fine!

RPMA=[]
for j in range (0,191600, 20):
    a=RPM.iloc[j:j+19]
    RPMA.append(a.mean())

Comments

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.