Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On Data Analysis with NumPy and Pandas

You're reading from  Hands-On Data Analysis with NumPy and Pandas

Product type Book
Published in Jun 2018
Publisher Packt
ISBN-13 9781789530797
Pages 168 pages
Edition 1st Edition
Languages
Author (1):
Curtis Miller Curtis Miller
Profile icon Curtis Miller
Toc

Index sorting


When talking about sorting, we need to think about what exactly we are sorting. There are rows, columns, their indices, and the data they contain. Let's first look at index sorting. We can use the sort_index method to rearrange the rows of a DataFrame so that the row indices are in order. We can also sort the columns by setting the access parameter of sort_index to 1. By default, sorting is done in ascending order; later rows have larger values than earlier rows, but we can change this behavior by setting the ascending value of the sort_index value to false. This sorts in descending order. By default, this is not done in place; you need to set the in place argument of sort_index to true for that.

While I have emphasized sorting for DataFrames, sorting a series is effectively the same. Let's see an example. After loading in NumPy and pandas, we create a DataFrame with values to sort, shown in the following screenshot:

Let's sort the index; notice that this is not done in place...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime