Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Sorting 2D Arrays | Commonly used NumPy Functions
Ultimate NumPy
course content

Зміст курсу

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

Sorting 2D Arrays

Here is an example of sorting a 2D array:

As you can see, simply passing our 2D array to the sort() function sorts each 1D array along the axis 1 (which is the default option in a 2D array). Setting axis=0 sorts each 1D array along the axis 0 (every column). Setting axis=None returns a contiguous sorted 1D array of all the elements of the 2D array.

Let’s now have a look at the code for our example:

12345678910
import numpy as np array_2d = np.array([[2, 9, 3], [1, 6, 4], [5, 7, 8]]) # Sorting a 2D array along axis 1 print(np.sort(array_2d)) print('-' * 20) # Sorting a 2D array along axis 0 print(np.sort(array_2d, axis=0)) print('-' * 20) # Creating a 1D sorted array out of the elements of array_2d print(np.sort(array_2d, axis=None))
copy

Sorting 2D Arrays in Descending Order

When sorting 2D arrays in descending order along a given axis, you need to use two slices: one full slice ([:]) and another with a negative step ([::-1]). The position of the slice with the negative step should correspond to the axis along which you are sorting.

Note

When sorting along axis 0, you can use only a slice with a negative step, as it already indexes along this axis.

Here is an example:

Let’s now have a look at the code for our example:

12345678910
import numpy as np array_2d = np.array([[2, 9, 3], [1, 6, 4], [5, 7, 8]]) # Sorting a 2D array along axis 1 in descending order print(np.sort(array_2d)[:, ::-1]) print('-' * 20) # Sorting a 2D array along axis 0 in descending order print(np.sort(array_2d, axis=0)[::-1]) print('-' * 20) # Creating a 1D sorted array out of the elements of array_2d in descending order print(np.sort(array_2d, axis=None)[::-1])
copy

You can always use NumPy documentation for reference: numpy.sort, ndarray.sort.

Завдання

You have a 2D array named exam_scores containing the scores for each exam from a certain subject. Each column represents a specific subject, and each row represents an individual student. Thus, a specific row displays the scores of that student for each exam. Your task is the following:

  1. Create a 2D NumPy array named top_scores_subject based on exam_scores where each column representing a certain subject should be sorted by scores in descending order:
    • use the appropriate NumPy function for sorting;
    • specify the correct array to sort as the first argument;
    • specify the second keyword argument to sort every column;
    • use the correct slices for descending order.
  2. Create a 1D NumPy array named sorted_scores based on exam_scores which contains all scores sorted in ascending order:
    • use the appropriate NumPy function for sorting;
    • specify the correct array to sort as the first argument;
    • specify the second keyword argument to create a contiguous sorted 1D array.

By doing so, we can easily see the highest score for each exam and the lowest scores out of all exams.

Завдання

You have a 2D array named exam_scores containing the scores for each exam from a certain subject. Each column represents a specific subject, and each row represents an individual student. Thus, a specific row displays the scores of that student for each exam. Your task is the following:

  1. Create a 2D NumPy array named top_scores_subject based on exam_scores where each column representing a certain subject should be sorted by scores in descending order:
    • use the appropriate NumPy function for sorting;
    • specify the correct array to sort as the first argument;
    • specify the second keyword argument to sort every column;
    • use the correct slices for descending order.
  2. Create a 1D NumPy array named sorted_scores based on exam_scores which contains all scores sorted in ascending order:
    • use the appropriate NumPy function for sorting;
    • specify the correct array to sort as the first argument;
    • specify the second keyword argument to create a contiguous sorted 1D array.

By doing so, we can easily see the highest score for each exam and the lowest scores out of all exams.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 3. Розділ 2
toggle bottom row

Sorting 2D Arrays

Here is an example of sorting a 2D array:

As you can see, simply passing our 2D array to the sort() function sorts each 1D array along the axis 1 (which is the default option in a 2D array). Setting axis=0 sorts each 1D array along the axis 0 (every column). Setting axis=None returns a contiguous sorted 1D array of all the elements of the 2D array.

Let’s now have a look at the code for our example:

12345678910
import numpy as np array_2d = np.array([[2, 9, 3], [1, 6, 4], [5, 7, 8]]) # Sorting a 2D array along axis 1 print(np.sort(array_2d)) print('-' * 20) # Sorting a 2D array along axis 0 print(np.sort(array_2d, axis=0)) print('-' * 20) # Creating a 1D sorted array out of the elements of array_2d print(np.sort(array_2d, axis=None))
copy

Sorting 2D Arrays in Descending Order

When sorting 2D arrays in descending order along a given axis, you need to use two slices: one full slice ([:]) and another with a negative step ([::-1]). The position of the slice with the negative step should correspond to the axis along which you are sorting.

Note

When sorting along axis 0, you can use only a slice with a negative step, as it already indexes along this axis.

Here is an example:

Let’s now have a look at the code for our example:

12345678910
import numpy as np array_2d = np.array([[2, 9, 3], [1, 6, 4], [5, 7, 8]]) # Sorting a 2D array along axis 1 in descending order print(np.sort(array_2d)[:, ::-1]) print('-' * 20) # Sorting a 2D array along axis 0 in descending order print(np.sort(array_2d, axis=0)[::-1]) print('-' * 20) # Creating a 1D sorted array out of the elements of array_2d in descending order print(np.sort(array_2d, axis=None)[::-1])
copy

You can always use NumPy documentation for reference: numpy.sort, ndarray.sort.

Завдання

You have a 2D array named exam_scores containing the scores for each exam from a certain subject. Each column represents a specific subject, and each row represents an individual student. Thus, a specific row displays the scores of that student for each exam. Your task is the following:

  1. Create a 2D NumPy array named top_scores_subject based on exam_scores where each column representing a certain subject should be sorted by scores in descending order:
    • use the appropriate NumPy function for sorting;
    • specify the correct array to sort as the first argument;
    • specify the second keyword argument to sort every column;
    • use the correct slices for descending order.
  2. Create a 1D NumPy array named sorted_scores based on exam_scores which contains all scores sorted in ascending order:
    • use the appropriate NumPy function for sorting;
    • specify the correct array to sort as the first argument;
    • specify the second keyword argument to create a contiguous sorted 1D array.

By doing so, we can easily see the highest score for each exam and the lowest scores out of all exams.

Завдання

You have a 2D array named exam_scores containing the scores for each exam from a certain subject. Each column represents a specific subject, and each row represents an individual student. Thus, a specific row displays the scores of that student for each exam. Your task is the following:

  1. Create a 2D NumPy array named top_scores_subject based on exam_scores where each column representing a certain subject should be sorted by scores in descending order:
    • use the appropriate NumPy function for sorting;
    • specify the correct array to sort as the first argument;
    • specify the second keyword argument to sort every column;
    • use the correct slices for descending order.
  2. Create a 1D NumPy array named sorted_scores based on exam_scores which contains all scores sorted in ascending order:
    • use the appropriate NumPy function for sorting;
    • specify the correct array to sort as the first argument;
    • specify the second keyword argument to create a contiguous sorted 1D array.

By doing so, we can easily see the highest score for each exam and the lowest scores out of all exams.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 3. Розділ 2
toggle bottom row

Sorting 2D Arrays

Here is an example of sorting a 2D array:

As you can see, simply passing our 2D array to the sort() function sorts each 1D array along the axis 1 (which is the default option in a 2D array). Setting axis=0 sorts each 1D array along the axis 0 (every column). Setting axis=None returns a contiguous sorted 1D array of all the elements of the 2D array.

Let’s now have a look at the code for our example:

12345678910
import numpy as np array_2d = np.array([[2, 9, 3], [1, 6, 4], [5, 7, 8]]) # Sorting a 2D array along axis 1 print(np.sort(array_2d)) print('-' * 20) # Sorting a 2D array along axis 0 print(np.sort(array_2d, axis=0)) print('-' * 20) # Creating a 1D sorted array out of the elements of array_2d print(np.sort(array_2d, axis=None))
copy

Sorting 2D Arrays in Descending Order

When sorting 2D arrays in descending order along a given axis, you need to use two slices: one full slice ([:]) and another with a negative step ([::-1]). The position of the slice with the negative step should correspond to the axis along which you are sorting.

Note

When sorting along axis 0, you can use only a slice with a negative step, as it already indexes along this axis.

Here is an example:

Let’s now have a look at the code for our example:

12345678910
import numpy as np array_2d = np.array([[2, 9, 3], [1, 6, 4], [5, 7, 8]]) # Sorting a 2D array along axis 1 in descending order print(np.sort(array_2d)[:, ::-1]) print('-' * 20) # Sorting a 2D array along axis 0 in descending order print(np.sort(array_2d, axis=0)[::-1]) print('-' * 20) # Creating a 1D sorted array out of the elements of array_2d in descending order print(np.sort(array_2d, axis=None)[::-1])
copy

You can always use NumPy documentation for reference: numpy.sort, ndarray.sort.

Завдання

You have a 2D array named exam_scores containing the scores for each exam from a certain subject. Each column represents a specific subject, and each row represents an individual student. Thus, a specific row displays the scores of that student for each exam. Your task is the following:

  1. Create a 2D NumPy array named top_scores_subject based on exam_scores where each column representing a certain subject should be sorted by scores in descending order:
    • use the appropriate NumPy function for sorting;
    • specify the correct array to sort as the first argument;
    • specify the second keyword argument to sort every column;
    • use the correct slices for descending order.
  2. Create a 1D NumPy array named sorted_scores based on exam_scores which contains all scores sorted in ascending order:
    • use the appropriate NumPy function for sorting;
    • specify the correct array to sort as the first argument;
    • specify the second keyword argument to create a contiguous sorted 1D array.

By doing so, we can easily see the highest score for each exam and the lowest scores out of all exams.

Завдання

You have a 2D array named exam_scores containing the scores for each exam from a certain subject. Each column represents a specific subject, and each row represents an individual student. Thus, a specific row displays the scores of that student for each exam. Your task is the following:

  1. Create a 2D NumPy array named top_scores_subject based on exam_scores where each column representing a certain subject should be sorted by scores in descending order:
    • use the appropriate NumPy function for sorting;
    • specify the correct array to sort as the first argument;
    • specify the second keyword argument to sort every column;
    • use the correct slices for descending order.
  2. Create a 1D NumPy array named sorted_scores based on exam_scores which contains all scores sorted in ascending order:
    • use the appropriate NumPy function for sorting;
    • specify the correct array to sort as the first argument;
    • specify the second keyword argument to create a contiguous sorted 1D array.

By doing so, we can easily see the highest score for each exam and the lowest scores out of all exams.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Here is an example of sorting a 2D array:

As you can see, simply passing our 2D array to the sort() function sorts each 1D array along the axis 1 (which is the default option in a 2D array). Setting axis=0 sorts each 1D array along the axis 0 (every column). Setting axis=None returns a contiguous sorted 1D array of all the elements of the 2D array.

Let’s now have a look at the code for our example:

12345678910
import numpy as np array_2d = np.array([[2, 9, 3], [1, 6, 4], [5, 7, 8]]) # Sorting a 2D array along axis 1 print(np.sort(array_2d)) print('-' * 20) # Sorting a 2D array along axis 0 print(np.sort(array_2d, axis=0)) print('-' * 20) # Creating a 1D sorted array out of the elements of array_2d print(np.sort(array_2d, axis=None))
copy

Sorting 2D Arrays in Descending Order

When sorting 2D arrays in descending order along a given axis, you need to use two slices: one full slice ([:]) and another with a negative step ([::-1]). The position of the slice with the negative step should correspond to the axis along which you are sorting.

Note

When sorting along axis 0, you can use only a slice with a negative step, as it already indexes along this axis.

Here is an example:

Let’s now have a look at the code for our example:

12345678910
import numpy as np array_2d = np.array([[2, 9, 3], [1, 6, 4], [5, 7, 8]]) # Sorting a 2D array along axis 1 in descending order print(np.sort(array_2d)[:, ::-1]) print('-' * 20) # Sorting a 2D array along axis 0 in descending order print(np.sort(array_2d, axis=0)[::-1]) print('-' * 20) # Creating a 1D sorted array out of the elements of array_2d in descending order print(np.sort(array_2d, axis=None)[::-1])
copy

You can always use NumPy documentation for reference: numpy.sort, ndarray.sort.

Завдання

You have a 2D array named exam_scores containing the scores for each exam from a certain subject. Each column represents a specific subject, and each row represents an individual student. Thus, a specific row displays the scores of that student for each exam. Your task is the following:

  1. Create a 2D NumPy array named top_scores_subject based on exam_scores where each column representing a certain subject should be sorted by scores in descending order:
    • use the appropriate NumPy function for sorting;
    • specify the correct array to sort as the first argument;
    • specify the second keyword argument to sort every column;
    • use the correct slices for descending order.
  2. Create a 1D NumPy array named sorted_scores based on exam_scores which contains all scores sorted in ascending order:
    • use the appropriate NumPy function for sorting;
    • specify the correct array to sort as the first argument;
    • specify the second keyword argument to create a contiguous sorted 1D array.

By doing so, we can easily see the highest score for each exam and the lowest scores out of all exams.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 3. Розділ 2
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt