Built with Tutify.io

    AIStudyLabs

    Pandas Series.sort_values() With Examples

    Pandas Series.sort_values() With Examples

    Pandas Series.sort_values() function is used to sort values on Series object. It sorts the series in ascending order or descending order, by default it does in ascending order. You can specify your preference using the ascending parameter which is True by default. In this article, you can find out how to sort values in pandas series with multiple examples.

    Key Points –

    <a id="h-1-quick-examples-of-sort-pandas-series"></a>

    1. Quick Examples of Sort Pandas Series

    If you are in hurry below are some quick examples of how to sort values of DataFrame by using Series.sort_values().

    
    # Quick examples of sort series
    
    # Sort pandas Series in an ascending order
    sortedSeries = mySeries.sort_values()
    sortedSeries = mySeries.sort_values(ascending=True)
    
    # Sort pandas Series in a descending order
    sortedSeries = mySeries.sort_values(ascending=False)
    
    # Sort inplace
    mySeries.sort_values(ascending=False, inplace=True)
    
    # Sort Series Contains Numeric Values
    import pandas as pd
    mySeries = pd.Series([25000,30000,23000,15000,80000])
    mySeries.sort_values(ascending=True)
    
    # Sort Series which contains NAN Values
    # Keeping NaN as first. By default it puts at last
    sortedSeries = Series.sort_values(ascending=True, na_position='first')
    print(sortedSeries)
    

    <a id="h-2-syntax-of-series-sort-values"></a>

    2. Syntax of Series.sort_values()

    Following is the syntax of the Series.sort_values(). This takes params axis, ascending, inplace, kind and na_position and returns a sorted Series object. With inplace=True param it returns None.

    
    # Syntax of Series.sort_values()
    Series.sort_values(axis=0, ascending=True, inplace=False, kind='quicksort', na_position='last', ignore_index=False, key=None)
    

    Now, Let's create a pandas series.

    
    # Create a pandas series.
    import pandas as pd
    series = pd.Series(['JAVA','PySpark','Spark','Hadoop'])
    print(series)
    

    Yields below output.

    
    # Output:
    0      Spark
    1    PySpark
    2       JAVA
    3     Hadoop
    dtype: object
    

    <a id="h-3-sort-the-series-in-ascending-order"></a>

    3. Sort the Series in Ascending Order

    By default, the pandas series sort_values() function sorts the series in ascending order. You can also use ascending=True param to explicitly specify to sort in ascending order. Also, if you have any NaN values in the Series, it sort by placing all NaN values at the end. You can change this behavior by using na_position param.

    
    # Sort the series in ascending order.
    sortedseries = series.sort_values()
    (or)
    sortedseries = series.sort_values(ascending=True)
    print(sortedseries)
    

    Yields below output.

    
    # Output:
    3     Hadoop
    0       JAVA
    1    PySpark
    2      Spark
    dtype: object
    

    Note that the sort_values() function returns a sorted copy of the original series. To modify the original series in-place, pass inplace=True to the function.

    
    # Sort the series in ascending order.
    series.sort_values(inplace=True)
    (or)
    series.sort_values(ascending=True,inplace=True)
    

    <a id="h-4-sort-series-in-descending-order"></a>

    4. Sort Series in Descending Order

    To sort a Pandas Series in descending order, you can use the Series.sort_values() method with the ascending parameter set to False.

    
    # Sort series in descending order.
    sortedseries = series.sort_values(ascending=False)
    print(sortedseries)
    

    In this example, ascending=False is used to sort the Series in descending order. The resulting sorted_series_desc will have the values sorted from the largest to the smallest. This example yields the below output.

    
    # Output:
    2      Spark
    1    PySpark
    0       JAVA
    3     Hadoop
    dtype: object
    

    <a id="h-5-sort-series-that-contains-numeric-values"></a>

    5. Sort Series that Contains Numeric Values

    Let’s now see how to sort a Series that includes numeric values. Now, create the following Pandas Series that contains only numeric values.

    
    # Sort Series Contains Numeric Values.
    import pandas as pd
    series = pd.Series([25000,30000,23000,15000,80000])
    print(series)
    

    Now, to sort a Pandas Series that contains numeric values, you can use the Series.sort_values() method. For instance, the sort_values() method is used to sort the series. The default behavior is to sort in ascending order, and you can set ascending=False to sort in descending order.

    
    # Sort the Series in an ascending order.
    sortedSeries = Series.sort_values(ascending=True)
    print(sortedSeries)
    

    Yields below output.

    
    # Output:
    3    15000
    2    23000
    0    25000
    1    30000
    4    80000
    dtype: int64
    

    <a id="h-6-sort-series-which-contains-nan-values"></a>

    6. Sort Series Which Contains NaN Values

    Now, let’s create a Series with NaN values using the Numpy library and then place the NaN values at the last while the other values will be sorted in ascending order. By default, it places all NaN values at the last.

    
    import pandas as pd
    import numpy as np
    # Create Series with NaN values
    Series = pd.Series([25000,np.NaN,23000,15000,80000])
    print(Series)
    
    # Sort Series which contains NAN Values.
    sortedSeries = Series.sort_values(ascending=True)
    print(sortedSeries)
    

    Yields below output.

    
    # Output:
    3    15000.0
    2    23000.0
    0    25000.0
    4    80000.0
    1        NaN
    dtype: float64
    

    Similarly, you can use the na_position parameter to control the placement of NaN values during sorting. If you want to place NaN values at the beginning when sorting in descending order, you can set na_position='first'.

    
    # Sort the Series by keeping NaN as first
    sortedSeries = Series.sort_values(ascending=True, na_position='first')
    print(sortedSeries)
    

    In the above example, na_position='first' ensures that NaN values are placed at the beginning of the sorted Series when sorting in descending order. This example yields the below output.

    
    # Output:
    1        NaN
    3    15000.0
    2    23000.0
    0    25000.0
    4    80000.0
    dtype: float64
    

    <a id="h-7-complete-examples-to-sort-series-contains-nan-values"></a>

    7. Complete Examples to Sort Series Contains NAN Values

    
    # Sort pandas Series in an ascending order:
    sortedSeries = mySeries.sort_values()
    sortedSeries = mySeries.sort_values(ascending=True)
    
    # Sort pandas Series in a descending order.
    sortedSeries = mySeries.sort_values(ascending=False)
    
    # Sort inplace
    mySeries.sort_values(ascending=False, inplace=True)
    
    # Sort Series Contains Numeric Values.
    import pandas as pd
    mySeries = pd.Series([25000,30000,23000,15000,80000])
    mySeries.sort_values(ascending=True)
    
    # Sort Series which contains NAN Values.
    # Keeping NaN as first. By default it puts at last
    sortedSeries = Series.sort_values(ascending=True, na_position='first')
    print(sortedSeries)
    

    <a id="h-frequently-asked-questions-on-pandas-series-sort-values-nbsp"></a>

    Frequently Asked Questions on Pandas Series.sort_values()

    The Series.sort_values() method in Pandas is used to sort the values of a Series in either ascending or descending order. It allows for flexible data manipulation and exploration by arranging values according to user-specified criteria.

    By default, Series.sort_values() performs stable sorting. In case of ties (equal values), the original order of those tied elements is preserved in the result. This ensures that the sorting operation is deterministic and reproducible.

    Series.sort_values() can be used with both numeric and non-numeric data. It is a versatile method that can handle sorting based on any comparable data type, including strings, dates, or custom objects, depending on the data contained in the Series.

    Series.sort_values() provides the inplace parameter. If inplace is set to False (the default), a new sorted Series is returned, leaving the original Series unchanged. Setting inplace=True modifies the original Series in place.

    The na_position parameter specifies the placement of NaN (Not a Number) values during sorting. It can be set to 'first' to place NaN values at the beginning of the sorted Series or 'last' to place them at the end. The default is 'last'.

    The method provides the key parameter, allowing you to specify a custom sorting function. This function is applied to each element in the Series, and the values are sorted based on the results of this function rather than the raw values themselves.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, you have learned how to sort values in pandas series by using Series.sort_values() It sorts the series in ascending order or descending order. By default, it does in ascending order and places all NaN values at the last.

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References