Sorting and Ranking Data

Lesson Introduction

Hey there! Today, we're diving into sorting and ranking data. These techniques help us organize and see patterns in our data, making it easier to analyze and draw conclusions. By the end of this lesson, you'll know how to sort data in different ways and rank data within groups using Pandas. We'll be working with the Titanic dataset, the same one we've used in previous lessons.

Sorting and ranking might sound a bit technical, but think of it like sorting your favorite toy collection by size or ranking your friends by age. It’s all about making data neat and meaningful!

Sorting Data

Sorting data means arranging it in a specific order, like alphabetizing words in a dictionary or listing numbers from smallest to largest. Let's start with some basics. Here's how to sort data by a single column. Suppose we want to sort passengers by how much they paid for their tickets (fare).

import seaborn as sns

# Load the Titanic dataset
titanic = sns.load_dataset('titanic')

# Sort by fare in descending order
titanic_sorted = titanic.sort_values(by='fare', ascending=False)
print(titanic_sorted[['fare', 'class']].head())

Output:

       fare   class
258  512.3292  First
680  512.3292  First
737  262.3750  First
27   263.0000  First
311  262.3750  First

Here, our data is sorted by fare in the descending order. We control it using by and ascending arguments of the sort_values function.

Imagine you're a librarian organizing books. Sorting helps you find books faster. Similarly, sorting data helps analysts focus on key information quickly, like the highest sales or the oldest customers.

Ranking Data

Ranking data means assigning a rank (like 1st, 2nd, 3rd) to items in your data based on their values. Let's use a simple dataset to make this clearer. Below is a small dataset of students and their scores.

import pandas as pd

# Sample dataset
data = {
    'student': ['Alice', 'Bob', 'Charlie', 'David'],
    'score': [88, 92, 85, 92]
}
students = pd.DataFrame(data)
print(students)

Output:

   student  score
0    Alice     88
1      Bob     92
2  Charlie     85
3    David     92

Now, let's see how to rank students by their scores.

# Rank students by their score
students['score_rank'] = students['score'].rank(method='average', ascending=True)
print(students)

Output:

   student  score  score_rank
0    Alice     88         2.0
1      Bob     92         3.5
2  Charlie     85         1.0
3    David     92         3.5

This table clearly shows how the students' scores are ranked. For example, Charlie has the lowest score, and he is ranked 1. Bob and David have a tie – they both hold the score of 92. We specified the tie handling method average. As Bob and David share ranks 3 and 4, their average rank is 3.5.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal