• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Sort Series in pandas with sort_values() Function

Sort Series in pandas with sort_values() Function

March 3, 2022 Leave a Comment

When working with series from the pandas module in Python, you can easily sort series using the sort_values() function.

s = pd.Series([11, 5, 30, 25, 14])

print(s.sort_values())

#Output:
1     5
0    11
4    14
3    25
2    30
dtype: int64

When working with data, it is very useful to be able to sort data in a list of items to order our data.

Many times when we are working with data in Python, we are using the pandas module and Series objects.

We can easily sort data in a Series object using pandas in Python.

To sort a pandas series, you can use the sort_values() function. The sort_values() function will sort a series by its values in ascending order.

Below is a simple example of how to use sort_values() on a pandas series to sort it by its values.

s = pd.Series([11, 5, 30, 25, 14])

print(s.sort_values())

#Output:
1     5
0    11
4    14
3    25
2    30
dtype: int64

If you want to modify the pandas series itself, you can pass “inplace=True”.

s = pd.Series([11, 5, 30, 25, 14])

s.sort_values(inplace=True)

print(s)

#Output:
1     5
0    11
4    14
3    25
2    30
dtype: int64

Sorting a pandas Series by Values Descending in Python

By default, the sort_values() function when applied to a pandas series sorts the series values in ascending order.

To sort a pandas series in descending order, pass “ascending=False” to sort_values().

Below is an example in Python of how to sort a pandas series descending.

s = pd.Series([11, 5, 30, 25, 14])

print(s.sort_values(ascending=False))

#Output:
2    30
3    25
4    14
0    11
1     5
dtype: int64

How to Sort Series in pandas using key Argument

Sometimes it makes sense to sort a series after the application of a function. We can use the sort_values() ‘key’ parameter to pass a function and sort by the function values.

For example, if we want to sort by the square of each number, we could pass a lambda expression that squares each number in a series.

Below is an example in Python of how to sort a pandas series with the ‘key’ parameter.

s = pd.Series([1, -5, 3, -4, 2])

print(s.sort_values(key=lambda x: x**2))

#Output:
0    1
4    2
2    3
3   -4
1   -5
dtype: int64

Treatment of NaN Values with pandas sort_values() Function

If your series has NaN values, you can specify the treatment of them after sorting using the ‘na_position’ parameter.

You can have NaN values go first or last in your series after sorting by passing ‘first’ and ‘last’ respectively to ‘na_position’.

By default, NaN values go last after sorting with sort_values().

Below is an example using the pandas module of sorting a series with NaN values.

s = pd.Series([1, np.nan, 3, -4, 2])

print(s.sort_values())

#Output:
3   -4.0
0    1.0
4    2.0
2    3.0
1    NaN
dtype: float64

You can have NaN values go first by passing ‘na_position=False’ as shown in the following Python code.

s = pd.Series([1, np.nan, 3, -4, 2])

print(s.sort_values(na_position=False))

#Output:
1    NaN
3   -4.0
0    1.0
4    2.0
2    3.0
dtype: float64

Sorting a pandas Series by Index in Python with sort_index()

If you want to sort the index of a pandas series, you can use the sort_index() function.

sort_index() sorts the index, and has all of the same parameters and keywords (ascending, inplace, key, etc.) as the sort_values() function.

Below is a simple example in Python of how to sort a pandas series by its index.

s = pd.Series(['a', 'b', 'c', 'd'], index=[2, 4, 1, 3])
print(s.sort_index())

#Output:
1    c
2    a
3    d
4    b
dtype: object

Hopefully this article has been useful for you to learn how to sort a series when using the pandas module in Python.

Other Articles You'll Also Like:

  • 1.  Create Unique List from List in Python
  • 2.  How to Group By Columns and Find Sum in pandas DataFrame
  • 3.  Touch File Python – How to Touch a File Using Python
  • 4.  Using Python to Repeat Characters in String
  • 5.  Comparing Datetimes in Python
  • 6.  Python Trig – Using Trigonometric Functions in Python for Trigonometry
  • 7.  Writing Multiple Lines Lambda Expression in Python
  • 8.  Concatenate Multiple Files Together in Python
  • 9.  Get Substring Between Two Characters with Python
  • 10.  pandas str replace – Replace Text in Dataframe with Regex Patterns

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy