Skip to playerSkip to main content
  • 22 minutes ago
Learn how mean, median, and mode reveal different insights about a dataset. Using customer lifetime value data from an online business, this beginner-friendly statistics lesson explains how to calculate each measure and work through the results in Python. You’ll see why the mean is the average, how the median is found from the middle value or pair of values, and why a dataset can have more than one mode.

The lesson also visualizes the data and shows how unusually high values can pull the mean upward, making the median a useful measure to compare when describing typical results. Explore introductory statistics, descriptive statistics, measures of central tendency, and practical data analysis with Python—an accessible guide for students and aspiring data professionals learning to interpret real-world data.

Category

📚
Learning
Transcript
00:00What's going on, everybody? Welcome back to another video. Today, we are continuing our
00:04statistics series. In this one, we're kind of starting with the basics. The last one was just
00:08an introduction to, you know, what kind of statistics data professionals use. Now we're
00:13starting to get hands-on, and this is kind of the basics, right? We're looking at mean, median,
00:19and mode. I am sure you guys have seen this before, but if you haven't, this is a great
00:24starting place, and then we'll get more advanced as we go throughout this series. The first one
00:29that we're going to look at is mean, because this is the one that almost everybody understands
00:32and knows. This is just your average. This is all of your values added together, and then
00:37divided by the count of how many rows you have. Next, we have the median, and this is probably
00:42the simplest one. This is just the very middle value of all of your values. And then lastly,
00:48we have mode, and mode is just your most common value. So if in your data set, you have the
00:54number 10 that is shown 100 times or in 100 different rows, and all the other ones are
00:58less than 100, then mode is your most common, because it has the highest number or is the
01:02most common value in that common. Now what we're about to do is we're about to pull up
01:06a data set, and we're going to calculate these and see how they actually work, so you understand
01:11their meaning and how to actually get them. Now for this entire series, we're going to
01:15be using Python. And the reason for that is that you can use other tools, even like Excel,
01:20for simple basic statistics. But as you get more advanced, and we will get more advanced
01:25in this series, I want you to be able to visualize and see these things. You just can't do that
01:31in Excel. So if you want to follow along, be sure to download Anaconda, get Jupyter Notebooks.
01:36I will have a link down in the description to download those. I also have an entire video
01:40on how to install and set that up. So be sure to go and check that out. I will also
01:45have all
01:45of the code, including the finalized code down below in a GitHub repository, so that you can just
01:51open it up and run it and understand it instead of having to write all the code yourself. This is
01:56not a coding tutorial. I'm just using Python in order to demonstrate how the statistics works
02:03with real data. So with that being said, all I'm doing is reading in some files up here and
02:08importing some libraries that we're going to be using. Now this is our data set. It's a fairly simple
02:14data set. We have the customer age, their gender, their region, their signup days, how long they signed
02:19up for our website, how much that lifetime value of this customer is, and their loyalty tier. The
02:25data that we're working with is assuming we have some type of online website-based business. Now the
02:31column that we're going to be looking at is this lifetime value right here. As you can see, we have
02:36lots of data, but if we just scroll down, these lifetime values vary quite widely. Some people are
02:43$600, some are just $30. And so there's a lot of variance here. Now what we're going to be doing
02:51is we're going to come right down here, and we're going to look at the mean, median, and mode. Now
02:55I'm pulling this up just so we have a reference of what we're trying to achieve here. I am using
03:01the
03:02mean, the median, and mode functions within Python. And I wouldn't say that's cheating, right? It's just
03:08built-in functions like you would use in Excel if you use the average function instead of calculating
03:13it out by hand. But we're going to calculate these out or at least be able to see how we
03:17got these
03:18numbers so you can understand it better. So for this, all we did was create this LTV pandas data
03:26frame. I can put this right here. If I just say LTV, and so you can see all we have
03:32is basically an array
03:32here. We have an index, and then we have the values. It doesn't even have column names because we're just
03:38working with one column, which is our lifetime value. I did that just to simplify it. So what
03:44we're going to do is we're going to use this array right here, and we're going to see how we
03:49can
03:49calculate these values. Now all I've done just to kind of help us out for a future one is I
03:55did
03:56something called sort values, basically from lowest all the way down to the very bottom, which is our
04:02highest value. One person's worth $13,782 to our business, whereas some are worth like $9. Okay,
04:11so we're going to see how we can calculate each of these. Let's start with the first one, which is
04:15just going to be our mean. And I'm just going to put it like this, which is mean. I'm just
04:20commenting
04:21it out. Now the mean is our average, and all we have to do is add up all of these
04:26values divided by the
04:28count of all of these values. Let's see how this looks. So let's come down here, and all we have
04:33to do is we're going to say LTV, that's just this right here. We're going to say LTV dot, and
04:40then
04:40we're going to add them all together, which is just the sum. Now if we run this, the sum of
04:46all these
04:46values is 1,900,262.20002. So it's about 1.9 million. Now all we have to do is take
04:59how many
04:59rows we have, and that's pretty easy. So we're going to divide by, and we do LTV dot count. So
05:08now
05:08we're taking how many rows we have. Now this is going to be about 5,000 rows. So we're taking
05:14this
05:141.9 million and dividing it by 5,000. Let's go ahead and run this, and that's where we get
05:21our
05:23380.05244, and it goes on and on. Now if we come up here, that is what we have right
05:28here. That's
05:29our 380.05. We're just rounding it to the second decimal point. So that's how we get average. I would
05:35say that's probably the most common one that most people have heard of and most people kind of
05:40understand, but that's how we calculate it by hand. I personally don't calculate it by hand very
05:45often, right? Most of the time I'm using some type of function like this. In almost any tool that I'm
05:51using, I'm not calculating it by hand. So I wouldn't in Excel, I wouldn't in Python, or R, or Tableau,
05:58or
05:58any other tool. Next, let's look at the median. So we're going to say median, and this is going to
06:05be
06:05our center value. Now this is actually really easy to determine. Now technically, we don't have
06:12to write any code for this at all. We could just come up here, and now one just odd thing
06:19is an
06:19index starts at zero, okay? If you're not into programming, that may not make sense, but when
06:24you have an index, it starts at zero. That's why when I said 5,000, it goes to 4,999.
06:30It's because
06:30if you add that zero into that one, it's 5,000. Now to determine our very middle value, we just
06:38go to
06:38the very center. That's going to be 2,000. Let's come up here. 499. So let's come up here, and
06:49we have
06:50right here, 251.94. Now wait a second. That median value is 251.99. Now since we have an even
07:00number,
07:01we have 5,000, 5,000 doesn't actually have a very center value. If it was odd, let's say 3,
07:08you have
07:081, and then you have 3, and 2 is your center. Now if you have 4, then we have 1,
07:142, 3, 4, and then 2 and 3
07:17are technically both center values, and that's what's happening right here. These are our two
07:22central values, which would be our median. And so what it does is if you have an even number of
07:28rows,
07:28which we have 5,000, it takes these two numbers, 251.94, 252.03, and it averages them. So what
07:37we get
07:37when we average them is this 251.99. Now let's actually write this out so that we can do this
07:45not just by sight, but by actually writing it out. Now we're going to take this. We're going to take
07:51this exact data set, and we want to have it sorted, right, so we can find that middle value that
07:56we
07:56have. Now I'm going to take this. I'm just going to give it a name. I'm going to say this
07:59is our sorted
08:01LTV. And then I'm going to do another thing. I'm going to say n is equal to our sorted underscore
08:09LTV.
08:10That's this right here. I'm going to say dot count, because we just need to get a count of it.
08:17Now the
08:17next thing we need to do is just determine kind of our center point. And we need to determine two
08:23separate values for this. We actually need our left side and our right side. And I'll explain that a
08:29second. But let's label this mid-right. And we'll say n. And we're going to do this right here. Now
08:36this is not a typical division, although we are just taking our count, which is our 5,000, and we're
08:42dividing it by two. But this right here is called a floor division. So if we just did like that,
08:49divided by two, if there were a decimal point, then it would say, you know, 2,500.26389. But if
08:56we add
08:56this, it's going to round down to that number. So now we can say, and let's get rid of that,
09:03mid
09:04underscore right, and let's run this. And it says that our middle right value is 2,500. And now what
09:12we're
09:12going to do is we're going to do the opposite of that. We're going to say our mid left. So
09:17this is now
09:17our left side. And we're going to say it's equal to the middle right, but then minus one. And so
09:26now
09:27let's look at our mid left. Our mid left is 2,499. So now that we have this, we can
09:35come right down
09:36here. And we can say our median is equal to our sorted LTV. Now this is just our values, right?
09:43These
09:43are our actual values. We're going to pull our 2499 value, which is 251.94 and 252.03. We're about
09:51to pull those in. So we're going to say LTV, but now we want to pull in just one value.
09:56That's our
09:57middle right. So we'll do that. And we're going to say plus our sorted middle left. And let's do it
10:05just like this. I'm going to left. And this actually looks like it's, it's not correct.
10:14This is a markdown file up here. I actually could have just made it code. And there we go.
10:19That works too. But sorry, I had it as a markdown for whatever reason. Now what we want to do
10:25is we
10:25want to add these values together first. So we're going to get the 251.94 plus 252.03. And then
10:34at the
10:35end, all we do is divide by two. So now let's run this. And now we get 251.985. Now
10:47you'll
10:47notice up here, when we come back, our median is 251.99. Again, it's just rounding this five
10:54up to be 0.99. But that is how we calculate the median right up here. Now lastly, we're going
11:04to look at the mode. And let's just come right here. We're going to say mode. Now mode is our
11:11most
11:12common value. And this is probably our easiest one to calculate. Now if you wanted to get super
11:18technical with it, like even in these ones, we technically, we don't have to use these functions
11:23or these sort values or things like that. We're using them because I think it's, you know, pretty
11:29evident. If you want to write out all the code, then it gets a little bit trickier. But
11:33for mode, basically what we're doing is we're seeing which ones have the highest count, right?
11:38We're going to group on a value. And we're going to say, okay, if this value 251.20 shows
11:45up 10 times, and all the other ones only show up five or three or one time, then this one
11:50has the highest count. And so let's come down here and let's see which one has our highest
11:55count for this. We're going to do LTV. We're just calling that data. We're going to say
11:59value underscore and do underscore counts. Let's go ahead and run this. And this function alone
12:08does 99% of the work. Now you're going to notice right up here, lifetime value, and we have our
12:14counts descending right from highest to lowest. Whoops. From highest to lowest. We have two that have
12:20three. Now if we go back up and we take a look, notice that our mode is 144.28. And
12:28then down here,
12:28we do have one that's 144.28. But we also have one that's 301.38. Now all I did up
12:37here, and I,
12:39you know, could have done it a different way, is you can actually have multiple values that are your
12:43mode. You don't have to have just one. I just selected one for, you know, simplicity. I'm taking the
12:49first index on the mode. So that's all that zero means. I'm taking that first index. So I selected
12:54the 144.28, but it could have been the 301.38. Now I could assign this a value. We don't
13:01have to
13:02keep going, but you know, why not? We're here. I'm going to say this is our counts, right? And then
13:06I'm going to say counts.max. I'm going to run this. And you can see three is our highest value.
13:14We have
13:14one that has three in it. And so I'll call this our max underscore count. And I need to get
13:23rid of
13:23this real quick. And so now we have three stored as our max count. So now we're just going to
13:28run
13:28this. We're going to say counts. And then within this, we're going to say the counts is equal to
13:34the max count. And then we'll say dot index. And let's run this. So now we're pulling in two values.
13:43Now I could put this in a data frame like this, so it looks a little prettier. But you get
13:48the point,
13:48right? We're working here and we're seeing here that these are our two highest values. Now I'm about
13:55to pull something over. You don't have to write this out. I'm just going to pull it over so we
14:01can look
14:01at it together. But let's go ahead and run this. So now I'm basically taking all that stuff that we've
14:07looked at, all of our mean, median, and mode, and I'm visualizing it. So this is our data. We have
14:13this
14:13nice little, it could be a bar graph or a histogram. I think this is a histogram because we have
14:18our bins
14:18here. But this is our histogram of our data. And we can see our mean right here. That's our average
14:25is about $3.80. It's about right here. Then we have our median. It's around $252. Again, that is
14:32rounded. But that's going to be our median right here. And then lastly, we have our mode. That's
14:38144. Or it could be the 301, which would be somewhere in between right here. Now this is just kind
14:46of a
14:47bonus at the end. But something that gets mixed up all the time and it drives me nuts is when
14:51people
14:52mix up mean and median. Because mean is just the average of all the values. And of course, this is
15:01really valuable. But I think averages get overused a lot. Whereas I feel like median, I don't feel,
15:08I know median is kind of a more true test for the actual data. And so when someone says, oh,
15:15the average
15:15person in America is making $180,000. And I'm like, wait, that is absurdly high. How are they getting
15:23this? Well, there's a few people in that data set that they're pulling from who are making like $100
15:27million a year. Whereas the median person is just making like $85,000 or $75,000 or $65,000. It's
15:34dramatically lower. But there's a few people on this far right side that bring up that average
15:40really high. And so when it comes to statistics, when we're working with real data like this,
15:45really, you need to understand that oops, you need to understand the difference between these two
15:50really well. And when you're presenting information, understand, you know, the difference and explain
15:56that to whoever you're working with. Because those numbers are often very different. If you made it all
16:01the way to the end, congratulations, you are on your way to understanding statistics a lot better.
16:06We have many more lessons coming. I think I'm going to have six or seven lessons in this series.
16:11And you will get a really good picture of statistics within data. With that being said,
16:16if you like this video, be sure to like and subscribe. I also have a full statistics course
16:20that you can take. I will have a link down in the description. And I will see you in the
16:24next lesson.
16:36Bye.

Recommended

  1. RareGear
    5 months ago