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

How to rank a Pandas DataFrame?

How to rank a Pandas DataFrame?

This recipe helps you rank a Pandas DataFrame

0
This python source code does the following : 1. Creates and converts data dictionary into pandas dataframe 2. Creates new columns in the dataframe 3. Ranks dataframe in ascending and descending order
In [1]:
## How to rank a Pandas DataFrame
def Kickstarter_Example_100():
    print()
    print(format('How to rank 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': [4, 24, 31, 2, 3],
            'coverage': [25, 94, 57, 62, 70]}

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

    # Create a new column that is the rank of the value of coverage in ascending order
    df['coverageRanked'] = df['coverage'].rank(ascending=True)
    print(); print(df)

    # Create a new column that is the rank of the value of coverage in descending order
    df['coverageRanked'] = df['coverage'].rank(ascending=False)
    print(); print(df)

Kickstarter_Example_100()
**************************How to rank a Pandas DataFrame**************************

             name  year  reports  coverage
Cochice     Jason  2012        4        25
Pima        Molly  2012       24        94
Santa Cruz   Tina  2013       31        57
Maricopa     Jake  2014        2        62
Yuma          Amy  2014        3        70

             name  year  reports  coverage  coverageRanked
Cochice     Jason  2012        4        25             1.0
Pima        Molly  2012       24        94             5.0
Santa Cruz   Tina  2013       31        57             2.0
Maricopa     Jake  2014        2        62             3.0
Yuma          Amy  2014        3        70             4.0

             name  year  reports  coverage  coverageRanked
Cochice     Jason  2012        4        25             5.0
Pima        Molly  2012       24        94             1.0
Santa Cruz   Tina  2013       31        57             4.0
Maricopa     Jake  2014        2        62             3.0
Yuma          Amy  2014        3        70             2.0

Relevant Projects

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.

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.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales 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.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

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.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.