How to rank a Pandas DataFrame?
0

How to rank a Pandas DataFrame?

This recipe helps you rank a Pandas DataFrame

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

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 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.
Big Data Project Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.
Big Data Project 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.
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 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.
Big Data Project 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.
Big Data Project 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.
Big Data Project 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.
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.