Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
Python | Pandas Series.min()
Next article icon

Sort a Pandas Series in Python

Last Updated : 05 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Series is a one-dimensional labeled array capable of holding data of the type integer, string, float, python objects, etc. The axis labels are collectively called index. 

Now, Let's see a program to sort a Pandas Series.

For sorting a pandas series the Series.sort_values() method is used.

Syntax: Series.sort_values(axis=0, ascending=True, inplace=False, kind=’quicksort’, na_position=’last’)Sorted

Returns: Sorted series

Examples 1: Sorting a numeric series in ascending order.

Python3
# importing pandas as pd import pandas as pd  # define a numeric series s = pd.Series([100, 200, 54.67,                 300.12, 400])  # print the unsorted series s 

  

Output:


 

series


 

Now we will use Series.sort_values() method to sort a numeric series in ascending order.


 

Python3
# sorting series s with  # s.sort_value() method in # ascending order sorted_series = s.sort_values(ascending                                = True) # print the sorted series sorted_series 

Output:

sorted series

From the output, we can see that the numeric series is sorted in ascending order.

Example 2:  Sorting a numeric series in descending order.

Python3
# importing pandas as pd import pandas as pd  # define a numeric series s = pd.Series([100, 200, 54.67,                 300.12, 400])  # print the unsorted series s 

  

Output:


 

series


 

Now we will use Series.sort_values() method to sort a numeric series in descending order.


 

Python3
# sorting the series s with  # s.sort_values() method # in descending order sorted_series = s.sort_values(ascending                               = False) # printing the sorted series sorted_series 

Output:

sorted series

From the output, we can see that the numeric series is sorted in descending order.

Example 3: Sorting a series of strings.

Python3
# importing pandas as pd import pandas as pd  #d efine a string series s s = pd.Series(["OS","DBMS","DAA",                "TOC","ML"])  # print the unsorted series s 

Output:

series

Now we will use Series.sort_values() method to sort a series of strings.

Python3
# sorting the series s with  # s.sort_values() method # in ascending order sorted_series = s.sort_values(ascending                                = True) # printing the sorted series sorted_series 

Output:

sorted series

From the output, we can see that the string series is sorted in a lexicographically ascending order.

Example 4: Sorting values inplace.

Python3
# importing numpy as np import numpy as np  # importing pandas as pd import pandas as pd  # define a numeric series # s with a NaN s = pd.Series([np.nan, 1, 3,                10, 5])  # print the unsorted series s 

Output:

series

Now we will use Series.sort_values() method to sort values inplace

Python3
# sorting the series s with  # s.sort_values() method in  # descending order and inplace s.sort_values(ascending  = False,                               inplace = True)  # printing the sorted series s 

  

Output:


 

so


 

The output shows that the inplace sorting in the Pandas Series.


 

Example 5: Sorting values in the series by putting NaN first.


 

Python3
# importing numpy as np import numpy as np  # importing pandas as pd import pandas as pd   # define a numeric series # s with a NaN s = pd.Series([np.nan, 1, 3,                10, 5])  # print the unsorted series s 

  

Output: 


 

series


 

Now we will use Series.sort_values() method to sort values in the series by putting NaN first.


 

Python3
# sorting the series s with  # s.sort_values() method in  # ascending order with na  # position at first sorted_series = s.sort_values(na_position =                               'first')  # printing the sorted series sorted_series 

  

Output:


 

sorted series


 

The output shows that the NaN (not a number) value is given the first place in the sorted series.


 


Next Article
Python | Pandas Series.min()

M

misraaakash1998
Improve
Article Tags :
  • Python
  • Python-pandas
  • Python pandas-series
Practice Tags :
  • python

Similar Reads

  • Python | Pandas Series.sort_index()
    Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.sort_index() function is used
    2 min read
  • Python | Pandas Series.set_axis()
    Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.set_axis() function is used t
    2 min read
  • Python | Pandas Series.min()
    Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.min() function return the mod
    2 min read
  • Python | Pandas Index.to_series()
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Index.to_series() function create a Series with both index and values equal to
    2 min read
  • Python | Pandas Series.argmin()
    Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.argmin() function returns the
    3 min read
  • Python | Pandas Series.mean()
    Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.mean() function return the me
    2 min read
  • Python | Pandas Series.to_dict()
    Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.to_dict() function is used to
    3 min read
  • Python | Pandas Series.ix
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas series is a One-dimensional ndarray with axis labels. The labels need not be un
    2 min read
  • Python | Pandas Series.at
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Series.at attribute enables us to access a single value for a row/column label
    2 min read
  • Python | Pandas Series.to_json()
    Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.to_json() function is used to
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences