How to sort rows within a Pandas DataFrame?
DATA MUNGING DATA CLEANING PYTHON MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to sort rows within a Pandas DataFrame?

How to sort rows within a Pandas DataFrame?

This recipe helps you sort rows within a Pandas DataFrame

Recipe Objective

Have you ever tried to sort a dataframe according to ascending or decending order with respect to features.

So this is the recipe on we can sort rows within a Pandas DataFrame.

Step 1 - Import the library

import pandas as pd

We have imported pandas which is needed.

Step 2 - Setting up the Data

We have created a dataset by making a dictionary with features and passing it through the dataframe function. raw_data = {"first_name": ["Sheldon", "Raj", "Leonard", "Howard", "Amy"], "last_name": ["Copper", "Koothrappali", "Hofstadter", "Wolowitz", "Fowler"], "age": [42, 38, 36, 41, 35], "Comedy_Score": [9, 7, 8, 8, 5], "Rating_Score": [25, 25, 49, 62, 70]} df = pd.DataFrame(raw_data, columns = ["first_name", "last_name", "age", "Comedy_Score", "Rating_Score"]) print(df)

Step 3 - Sorting the dataset

For better understanding we are first sorting the dataset with respect to age and in decending order. Then again with respect to age in ascending order. Finally with respect to both first age then Comedy_Score. print(df.sort_values(by="age", ascending=0)) print(df.sort_values(by="age", ascending=1)) print(df.sort_values(by=["age", "Comedy_Score"])) So the output comes as

  first_name     last_name  age  Comedy_Score  Rating_Score
0    Sheldon        Copper   42             9            25
1        Raj  Koothrappali   38             7            25
2    Leonard    Hofstadter   36             8            49
3     Howard      Wolowitz   41             8            62
4        Amy        Fowler   35             5            70

  first_name     last_name  age  Comedy_Score  Rating_Score
0    Sheldon        Copper   42             9            25
3     Howard      Wolowitz   41             8            62
1        Raj  Koothrappali   38             7            25
2    Leonard    Hofstadter   36             8            49
4        Amy        Fowler   35             5            70

  first_name     last_name  age  Comedy_Score  Rating_Score
4        Amy        Fowler   35             5            70
2    Leonard    Hofstadter   36             8            49
1        Raj  Koothrappali   38             7            25
3     Howard      Wolowitz   41             8            62
0    Sheldon        Copper   42             9            25

  first_name     last_name  age  Comedy_Score  Rating_Score
4        Amy        Fowler   35             5            70
2    Leonard    Hofstadter   36             8            49
1        Raj  Koothrappali   38             7            25
3     Howard      Wolowitz   41             8            62
0    Sheldon        Copper   42             9            25
‚Äč

Download Materials

Relevant Projects

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.

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.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

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.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Churn Prediction in Telecom using Machine Learning in R
Estimating churners before they discontinue using a product or service is extremely important. In this ML project, you will develop a churn prediction model in telecom to predict customers who are most likely subject to churn.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.