Hello, and welcome to a video on frequency tables in Python. First, let's load in some data. And we'll do this just
as we usually do. You may notice I'm importing NumPy as NP, and that's for an example that I'll be using below, but
it's not necessary to load in your data or make a frequency table. Let's go ahead and run this block of code.
And now we can see I've got IV1, IV2, and DV available to use. Next,
let's take a look at our data using a frequency table. Now is a great time to talk about methods versus functions.
Think of a function as a tool you use by calling its name and giving it some input inside parentheses.
For example, NP.mean, open parentheses, D-F-IV2, close parentheses,
is a function that calculates the mean of the column IV2. The function is wrapped around the data it operates
on, and we can see this below. Print is also a function.
A method is like a function, but it's attached to a specific object, like a variable or data frame,
and is applied directly to it. For example, .value_counts() is a method that you apply to a
column, like df['IV2'], to count the occurrences of each unique value. You write
it after the variable with a dot in between. So here
we're going to make a frequency table. And we're going to use two methods to create our frequency table data frame.
First, we have dot value counts, which does exactly what you think it's going to do.
It counts the values. Next, we have dot reset index, which is a little more obscure in what it's doing.
It adds the value counts we just made to our data frame as a new column.
Then we'll name our columns right here using the dot columns
method.
And there you go, you see we've got our frequencies. We have a 96,
20 times in our data, 92, appears 16 times, 80, appears 11 times and so
on and so forth. Great. So that's a frequency table. But what
if we wanted it to be an ascending order? Because this is a descending order by default, 96 is
at the top and then it goes down all the way to 78.75. We'll add another method
in between the two we already have called dot sort index. So now our code is just slightly different.
We have dot value counts same as before followed by dot sort index followed by dot reset index.
We'll name our columns again using the dot columns and print our frequency table.
Okay, great. This time we can see it starts at 50 and it ends at 96.
Now it's an ascending order. Neat. But what if we want to add a column for cumulative percentage? While
there's not a quick way to do this in Python, we can use a formula to help us create a new column in our data frame to do just that.
You'll notice we have our new methods in this snippet of code.cumulative sum and dot sum.
So first we'll make our frequency table, same as before, and then we'll add that cumulative
percentage. We're going to create a column in frequency table called cumulative percentage.
And then in frequency table, we're going to take frequency, the variable that we defined above right
here, apply the dot cumulative sum method to it,
divide it by frequency table, frequency dot sum times 100.
And for your variables, the only thing that will change is
the variable that you put here. Everything else can stay the same. Now, let's print our frequency table.
And we can see we have a cumulative percentage and it's an ascending order.
And that's it. That should be enough to get you started on making your own frequency table.