Locked lesson.
About this lesson
In this video, we discuss how to find unique values within a column and how to find value counts as well.
Exercise files
Download this lesson’s related exercise files.
Operations: Unique Values and Value Counts.docx57.3 KB Operations: Unique Values and Value Counts - Solution.docx
55.5 KB
Quick reference
Operations: Unique Values and Value Counts
In this video we'll learn how to return unique values of columns and counts of unique values.
When to use
Use these two functions to return unique values of a column, and to count the number of unique values in a column.
Instructions
To return a Numpy Array of Unique Values in a column:
my_df["Wed"].unique()
To return a DataFrame of Unique Values in a column:
pd.DataFrame(my_df["Wed"].unique())
To count how many Unique Values are in a column:
my_df["Wed"].nunique()
Hints & tips
- Unique Values: my_df["Wed"].unique()
- my_df["Wed"].nunique()
- 00:05 Okay, in this video I want to talk about how to find unique values in a specific
- 00:09 column and how to find those value counts as well.
- 00:12 So let's create a new data frame really quick, let's call it my_df.
- 00:17 And we should be getting good at this now that we've done so many of these.
- 00:24 And we're just going to use a dictionary to create this really quickly.
- 00:25 And let's create a column called Mon, and inside of Mon let's put 1, 2, 3, and 4.
- 00:32 And let's create another column called Tues, and
- 00:36 inside of Tues let's just put 5, 6, 7 and 8.
- 00:40 And let's create a column called Wed.
- 00:44 And inside of Wed let's put some different data,
- 00:49 let's go 111, 222, 333, and 111.
- 00:53 So we're mixing it up a little bit here.
- 00:56 Now let's look at this real quick.
- 00:57 So let's call my_df, and we get Mon, Tues, Wed columns.
- 01:01 And we don't really care about Mon and Tues,
- 01:05 I really want to focus just on Wed here.
- 01:08 So you see we have 111, 222, 333, and then 111 again.
- 01:12 So this first three, 111, 222, and 333, those are unique values.
- 01:16 But this fourth one, 111, that's not unique, we've already seen it before.
- 01:20 So how do we count how many things are unique in a column?
- 01:23 Well, we can use the unique function.
- 01:25 So we can call my_df, and we can pass in whatever column we want to check.
- 01:30 So we want to check Wed.
- 01:31 And then we could just run .unique as a function off of it.
- 01:35 And we get this array and
- 01:37 it looks like a NumPy array, which is weird because NumPy just keeps popping up.
- 01:42 And it just shows the unique values 111, 222, and 333.
- 01:45 Now there are four values in this column but
- 01:47 we're only seeing three because only three of them are unique.
- 01:51 Now this looks like a NumPy array and
- 01:53 let's run our type command here that we did earlier.
- 01:55 And we can see sure enough, yep, that is a NumPy array.
- 01:58 A Numpy just keeps popping up, which is fun.
- 02:01 This is being returned as an array, we could always just create a data frame.
- 02:06 We could go pd.DataFrame and just pass in this whole thing,
- 02:11 right, if we wanted it to look a little more fancy.
- 02:15 So that's how to find the unique values.
- 02:18 Now if we want to know how many unique there are,
- 02:20 now we can see at a glance that there are three of these.
- 02:23 But your data you may have thousands of rows,
- 02:25 millions of rows, you're not going to go through and count each one.
- 02:29 If you wanted a count of how many unique values there are,
- 02:32 there are a couple of ways you could do it.
- 02:34 First, you could call the len feature, the len function, it stands for length, right?
- 02:40 And this will tell us there are in fact three unique values, this one, this one,
- 02:45 and this one.
- 02:46 This one is not unique, right?
- 02:49 So that's sort of a hacky way to do it.
- 02:50 You could also use the nunique function that comes with pandas,
- 02:54 I think it just stands for number of unique, right?
- 02:58 So if we do that we get the same three.
- 03:00 So that's how to find unique and the number of unique.
- 03:03 What about a count of each value?
- 03:07 Well, that's a value count.
- 03:09 We can run value_counts, which is a function.
- 03:14 And here we get the series that shows how many of each one.
- 03:20 So there are two 111s, right, one and two.
- 03:24 There's only one 222 and there's only one 333.
- 03:29 And again, we could pass this in to a data frame if we
- 03:34 wanted to make this nicer to look at.
- 03:37 And we could see there are in fact two 111s and the rest will only have one.
- 03:43 So that's a quick and easy way to find the number of values that are unique and
- 03:48 not unique at a glance.
- 03:50 So that's kind of cool.
- 03:52 So pretty simple to find unique values and their value counts using pandas.
- 03:56 In the next video we'll look at the applied method.
Lesson notes are only available for subscribers.