Locked lesson.
About this lesson
In this video, we discuss how to sort and order your data.
Exercise files
Download this lesson’s related exercise files.
Operations: Sorting, Ordering, and Finding Nulls.docx57.2 KB Operations: Sorting, Ordering, and Finding Nulls - Solution.docx
55.4 KB
Quick reference
Operations: Sorting, Ordering, and Finding Nulls
Sorting can be done using the sort_values() function.
When to use
Use the sort_values function to sort and order data in a DataFrame.
Instructions
To sort data in a particular column:
my_df.sort_values('Wed')
To sort data in a particular column in descending order:
my_df.sort_values('Wed', ascending=False)
Hints & tips
- Ascending: my_df.sort_values('Wed')
- Descending: my_df.sort_values('Wed', ascending=False)
- 00:05 Okay, in this video I want to talk pretty quickly about sorting and
- 00:08 ordering your data.
- 00:09 So we've got, let's just use our current data frame, and we've got Monday, Tuesday,
- 00:14 Wednesday along the top where we've got our just numbered indexed as the row
- 00:18 headings here.
- 00:19 And you'll notice on Wednesday, we still have our 111 and
- 00:22 we've also got 111 down here.
- 00:24 So let's say we wanted to order Wednesday in numerical order with all
- 00:29 of the similar things bunched together and just in order from lowest to greatest,
- 00:34 we could do that using the sort values function.
- 00:37 So we could just go my_df.sort_values and
- 00:43 if we shift tab, we see, let's make this bigger here.
- 00:47 We can see you can sort by whatever you want to sort by and
- 00:52 in our case, we're going to pick Wednesday.
- 00:55 The x zero is it's going to sort by that by default.
- 00:58 Ascending true in place equals false, kind equal quick sort, in position equals last,
- 01:05 we don't really care about these last two things but we've seen this in
- 01:08 place many times and we could go ascending true or false and we'll look at that.
- 01:12 So let's go ahead and close this.
- 01:13 So we want to sort by, let's say Wednesday, and if we run this, we see 111,
- 01:18 111, 222 and 333.
- 01:20 So it's sorted them in order from low to high in ascending order.
- 01:26 We could go ascending equals False and
- 01:32 now it does the opposite from high to low 333, 222, 1111.
- 01:36 So that's a quick way to sort values.
- 01:39 Now we did that with numbers, you could do the same thing with strings.
- 01:42 So let's head back up to our data frame up here and
- 01:47 let's change Monday from 1, 2, 3, 4,
- 01:52 to let's go John, Sally, Bob and Tina.
- 01:57 We shift run we get John, Sally, Bob and Tina.
- 02:00 Now if we want to sort by Monday, we see it's been rearranged to Bob,
- 02:06 John, Sally, Tina, and you'll notice it's still ascending but
- 02:10 it's ascending in alphabetical order.
- 02:12 So B is the first letter in the alphabet in our list at least than J, S and
- 02:17 T and you can see the index numbers here changed to reflect that.
- 02:22 So pretty interesting.
- 02:23 Now, we have all have different values Bob, John, Sally and Tina.
- 02:28 If we came up here and change Tina to John and shift around this and
- 02:32 then I did this again, we would get Bob, John, John, Sally because it's going to
- 02:38 group the two Johns together because they're the same value, and that's cool.
- 02:42 And just like before we can change this ascending to false and
- 02:48 it's just going to change the order from the highest letter in the alphabet S,
- 02:54 down to J, J and then B for Bob.
- 02:57 So, quick and easy way to sort values.
- 03:00 Pretty cool and pretty useful and
- 03:02 that is pretty much the end of the sort of hardcore pandas section.
- 03:06 We learned a lot.
- 03:07 We went over a lot of sort of dense stuff in this section and
- 03:11 it may take a little bit of time for this all to sink in but
- 03:14 the more you play around with this, the easier this becomes.
- 03:17 You start creating these data frames like it was nothing and
- 03:20 as you do things with them, it just really becomes second nature.
- 03:24 In the next section, we're going to move into pandas visualization,
- 03:27 we're going to start looking at charts and graphs and all kinds of graphics and
- 03:30 cool stuff like that and that'll be coming up in the next video.
Lesson notes are only available for subscribers.