How to sort rows within a Pandas DataFrame?
0

How to sort rows within a Pandas DataFrame?

This recipe helps you sort rows within a Pandas DataFrame

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

Big Data Project Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.
Big Data Project 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.
Big Data Project Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.
Big Data Project Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.
Big Data Project 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.
Big Data Project Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.
Big Data Project 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.
Big Data Project Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.
Big Data Project Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.
Big Data Project 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.