How to RANK in a Pandas DataFrame in Python

January 13, 2019

Contact Seller

Guest Post Description

How to RANK in a Pandas DataFrame in Python

def Kickstarter_Example_100a(): 
    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_100a()

How to RANK in a Pandas DataFrame in Python

Add a Review

You must be logged in to post a comment.