how do you calculate the arithmetic mean of the number in django ORM ?

Django: assuming you have a Page model with visits_count (IntegerField) field, how do you calculate the arithmetic mean of the number of visits for all pages in the database?

Pay attention, that visits_count is Integer Field

If you’re talking about doing it within a query, Cast the visit_counts to a FloatField before dividing by the number of rows.

1 Like

I cant understand what do you mean by dividing by the number of rows. Can I get a code? Sorry for the stupid questions, but Im a newbie in this.Thanks in advance Sir

What’s the definition of “the arithmetic mean”?

My bad
Arithmetical mean
In other word - average

Yes, I understand that. But what is the defintion of an average? Or, to phrase it differently, how would you calculate the average of a set of numbers?

the number of users visited per page

Mathematically, what is the formula for calculating an average?

avg=(a1+a2+a3+a4+…+an)/n

Ok, so what does “n” represent in this specific case? And how do you think it relates to your earlier question?

If I`m not wrong it means “visits”

No, that’s not quite correct.

Let’s say I have 5 numbers:
2, 6, 8, 14, 20

What is the average of these 5 numbers?

What then is the “n” in your formula?

1.(2+6+8+14+20)/5 = 10 is the average of these numbers
2. amount of pages

Hope I replied correctly this time

Correct! So, in your formula, you’re creating the sum of a set of numbers, and then dividing that by the number of numbers in that set.

In the case of the problem you’re trying to solve, the “number of numbers in the set” is the number of different pages for which you have a “visit_count”, or, in other words, the number of rows (or number of “entries” if you prefer) in your Page model.

And that is what I mean by:

yeah, that makes sense right now
thanks for the calmness and for working with me

1 Like