Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Using Sort Methods to Modify Your DataFrame

For more information on concepts covered in this lesson, you can check out Sorting Data With Python.

00:00 Using Sort Methods to Modify Your DataFrame. In all the examples you’ve seen so far, both .sort_values() and sort.index() have returned DataFrame objects when you called those methods.

00:13 That’s because sorting in pandas doesn’t work in-place by default. In general, this is the most common and preferred way to analyze your data with pandas since it creates a new DataFrame, instead of modifying the original.

00:26 This allows you to preserve the state of the data from when you read it from the file. However, you can modify the original DataFrame directly by specifying the optional parameter inplace with the value of True.

00:40 The majority of pandas methods include the inplace parameter. In this section, you’ll see a few examples of using inplace=True to sort your DataFrame in-place.

00:52 With inplace set to True, you’re modifying the original DataFrame, so the sort methods return NaN. Sort your DataFrame by the values of the city08 column like the very first example, but with inplace set to True, as seen on-screen.

01:12 Notice that calling .sort_values() doesn’t return a DataFrame. Here’s what the original DataFrame looks like. In the DataFrame object, the values are now sorted in ascending order, based on the city08 column.

01:27 The original DataFrame has been modified, and the changes will persist.

01:33 It’s generally a good idea to avoid using inplace=True for analysis because the changes to your DataFrame can’t be undone. The next example illustrates that inplace also works with .sort_index().

01:47 Since the index was created in ascending order when you read your file into a DataFrame, you can modify the DataFrame object again to get it back to its original order.

01:58 Use .sort_index() with inplace=True to modify the DataFrame.

02:08 Now, the DataFrame has been modified again using .sort_index(), and since the DataFrame still has the default index, sorting it in ascending order puts the data back into the original order.

02:21 If you’re familiar with Python’s built-in functions sort() and sorted(), then the inplace parameter available in the pandas sort methods might seem familiar. For more information, check out this Real Python course.

02:37 In the final part of the course, you’ll take a look back at what you’ve learned.

Become a Member to join the conversation.