How to sort an array by the nth column?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to sort an array by the nth column?

How to sort an array by the nth column?

This recipe helps you sort an array by the nth column

0

Recipe Objective

While handling arrays, we might need to sort array on certain columns.

So this recipe is a short example on how to sort an array by the nth column. Let's get started.

Step 1 - Import the library

import numpy as np

Let's pause and look at these imports. Numpy is generally helpful in data manipulation while working with arrays. It also helps in performing mathematical operation.

Step 2 - Defining random array

a = np.array([[9, 2, 3],[4, 5, 6],[7, 0, 5]])

We have created a random array.

Step 3 - Sorting and printing array

a = a[a[:,1].argsort()] print(a)

We now use argsort feature to sort on any column. Here, we are sorting on 2nd column.

Step 4 - Lets look at our dataset now

Once we run the above code snippet, we will see:

Scroll down to the ipython file below to visualize the output.

Relevant Projects

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

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.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

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.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.