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

0
This python source code does the following: 1. Creates data dictionary and converts it into pandas dataframe 2. Sorts dataframe into ascending and descending format using a column as basis 3. Sorts datagram using multiple column as basis
In [1]:
## How to sort rows within a Pandas DataFrame
def Snippet_109():
    print()
    print(format('How to sort rows within a Pandas DataFrame','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    import pandas as pd

    # Create dataframe
    data = {'name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
            'year': [2012, 2012, 2013, 2014, 2014],
            'reports': [1, 2, 1, 2, 3],
            'coverage': [2, 2, 3, 3, 3]}

    df = pd.DataFrame(data, index = ['Cochice', 'Pima', 'Santa Cruz', 'Maricopa', 'Yuma'])
    print(); print(df)

    # Sort the dataframe’s rows by reports, in descending order
    print(); print(df.sort_values(by='reports', ascending=0))

    # Sort the dataframe’s rows by reports, in ascending order
    print(); print(df.sort_values(by='reports', ascending=1))

    # Sort the dataframe’s rows by coverage and then by reports, in ascending order
    print(); print(df.sort_values(by=['coverage', 'reports']))

Snippet_109()
********************How to sort rows within a Pandas DataFrame********************

             name  year  reports  coverage
Cochice     Jason  2012        1         2
Pima        Molly  2012        2         2
Santa Cruz   Tina  2013        1         3
Maricopa     Jake  2014        2         3
Yuma          Amy  2014        3         3

             name  year  reports  coverage
Yuma          Amy  2014        3         3
Pima        Molly  2012        2         2
Maricopa     Jake  2014        2         3
Cochice     Jason  2012        1         2
Santa Cruz   Tina  2013        1         3

             name  year  reports  coverage
Cochice     Jason  2012        1         2
Santa Cruz   Tina  2013        1         3
Pima        Molly  2012        2         2
Maricopa     Jake  2014        2         3
Yuma          Amy  2014        3         3

             name  year  reports  coverage
Cochice     Jason  2012        1         2
Pima        Molly  2012        2         2
Santa Cruz   Tina  2013        1         3
Maricopa     Jake  2014        2         3
Yuma          Amy  2014        3         3

Relevant Projects

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

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
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.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

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.

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.

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.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.