How to sort a 2D array by a column in numpy?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to sort a 2D array by a column in numpy?

How to sort a 2D array by a column in numpy?

This recipe helps you sort a 2D array by a column in numpy

0

Recipe Objective

How to sort a 2D array by a column. yes we can do this and for that we have to use the "sort" function available in the numpy library. In this if we want to sort 2D numpy array by 2nd column then we have to change the positions of all the rows based on the sorted values of the column (2nd column) with an column index for e.g 1 we can say.

Step 1 - Import library

import numpy as np

Step 2 - Take a Sample array

Sample_array = np.array([[100, 101, 500, 104], [201, 202, 203, 204], [301, 300, 600, 307]]) print("This is Sample 2D array :","\n", Sample_array)
This is Sample 2D array : 
 [[100 101 500 104]
 [201 202 203 204]
 [301 300 600 307]]

Step 3 - Sort Sample array

Index = 2 Array_sort = Sample_array[Sample_array[:,Index].argsort()] print("The original array is:","\n","\n", Sample_array, "\n") print("The sorted array is:", "\n", "\n", Array_sort)
The original array is: 
 
 [[100 101 500 104]
 [201 202 203 204]
 [301 300 600 307]] 

The sorted array is: 
 
 [[201 202 203 204]
 [100 101 500 104]
 [301 300 600 307]]

Here we can see the array has been sorted according to the given Index number that we have passed in "Indexed" variable i.e 2. So we can see the 3rd column is been sorted now as before it was not.

Relevant Projects

Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.