Sorting datasets based on multiple columns using sort_values

Home » News » Sorting datasets based on multiple columns using sort_values
You can easily sort your dataset based on single or multiple columns. First you need to load your data into pandas dataframe and then use sort_values function to sort the dataset.


Consider a Load Prediction dataset. We will try to sort this dataset based upon Applicant and Coapplicant income in both ascending and descending order.

Step 1: Import the required libraries

import pandas as pd
import numpy as np

Step 2: Load the dataset

dataset = pd.read_csv(“C:/train_loan_prediction.csv”)


Step 3: Sort the dataset in ascending and descending order


Lets sort our dataset based on the Applicant Income and Coapplicant Income.


dataset_sorted = dataset.sort_values([‘ApplicantIncome','CoapplicantIncome'])
dataset_sorted[[‘ApplicantIncome','CoapplicantIncome']].head(50)


Above code will sort the dataset based on multiple columns (ApplicantIncome and CoapplicantIncome) in ascending order. 


If you want to sort the dataset in descending order, just add ascending=False in sort_values function like this:


dataset_sorted = dataset.sort_values([‘ApplicantIncome','CoapplicantIncome'], ascending=False)
dataset_sorted[[‘ApplicantIncome','CoapplicantIncome']].head(50)


Now it will show the data in descending order. Data of applicants with higher income is displayed first.

Leave a Reply

Your email address will not be published. Required fields are marked *

New Providers
Binolla

The Broker
More then 2 million businesses
See Top 10 Broker

gamehag

Online game
More then 2 million businesses
See Top 10 Free Online Games

New Games
Lies of P

$59.99 Standard Edition
28% Save Discounts
See Top 10 Provider Games

COCOON

$24.99 Standard Edition
28% Save Discounts
See Top 10 Provider Games

New Offers
Commission up to $1850 for active user of affiliate program By Exness

Top Points © Copyright 2023 | By Topoin.com Media LLC.
Topoin.info is a site for reviewing the best and most trusted products, bonus, offers, business service providers and companies of all time.

Discover more from Top Points

Subscribe now to keep reading and get access to the full archive.

Continue reading