Pandas Series.sort_values() With Examples

  • Post author:
  • Post category:Pandas
  • Post last modified:November 16, 2023

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.

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().


# Below are some quick examples.

# 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)

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)
  • axis : Axis of sorting (rows/columns).
  • ascending : If True, sort values in ascending order, otherwise descending.
  • inplace : If True, perform operation in-place.
  • kind : Specify what sorting algorithm to use.
  • na_position : Where is place NaN values first or last.

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

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)

4. Sort Series in Descending Order

To sort the above series in descending order, use the sort_values() function with ascending=False.


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

Yields below output.


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

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, Sort the Series in ascending 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

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

You also set na_position=’first’ to sort by placing all NaN values at the first.


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

Yields


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

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)

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.

References

Leave a Reply

You are currently viewing Pandas Series.sort_values() With Examples