-2

I need to calculate [mean] for only some particular variable in a data set. I have a data frame in python contain variables like ID,Name,Jan,Feb,Mar,Apr,June. With this variable i have to create a new variable like "Avgmnth". by calculating the mean only for Feb,Mar,Apr

How to calculate this in python.Help me in this

1 Answers1

1

Try to use the search function next time. If I understood your question correct you're looking for this?

As answered by @Herms

If your reduce is already returning your sum, then all you have left to do is divide.

l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print reduce(lambda x, y: x + y, l) / len(l)

though sum(l)/len(l) would be simpler, as you wouldn't need a lambda.

If you want a more exact float result instead of an int then just use float(len(l)) instead of len(l).

Community
  • 1
  • 1
товіаѕ
  • 2,881
  • 4
  • 23
  • 53