Skip to content
Learn Netverks

Lesson

Step 13/36 36% through track

sorting-ranking

Sorting and ranking

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Sorting and ranking: Pandas tabular manipulation—indexing, dtypes, reshaping, and analysis habits for real-world tables.

Teams apply Sorting and ranking in every serious Pandas project—skipping it leaves blind spots in analysis and reviews.

You will apply Sorting and ranking in contexts like: CSV/Parquet analysis, ETL notebooks, and ad hoc reporting.

Read the narrative, run `import pandas as pd` snippets with in-memory DataFrames (install pandas and numpy with pip if needed), inspect `.head()`, `.dtypes`, and complete MCQs.

When you can explain the previous lesson's ideas in your own words.

Sort rows with sort_values and columns with sort_index(axis=1). Add rank columns with rank() for percentiles, competition ranking, or deduplication tie-breaking.

sort_values

import pandas as pd
df = pd.DataFrame({'name': ['C','A','B'], 'score': [90, 85, 90]})
by_score = df.sort_values('score', ascending=False)
print(by_score)

Multi-column sort

df.sort_values(['score', 'name'], ascending=[False, True])

rank methods

  • rank(method='average') — ties get average rank (default)
  • method='min' — competition ranking (1, 2, 2, 4)
  • pct=True — percentile rank 0–1
  • df.reset_index(drop=True) — clean 0..n-1 index after sort

Important interview questions and answers

  1. Q: Stable sort?
    A: Pandas sort is stable—equal keys preserve original order.
  2. Q: rank vs sort?
    A: Sort reorders rows; rank adds numeric position column without reordering.

Self-check

  1. Sort by two columns with different ascending flags.
  2. Add a percentile rank column.

Tip: After sort_values, call reset_index(drop=True) for a clean 0..n-1 index.

Interview prep

Stable sort?

Equal keys preserve original order in Pandas sort.

rank pct?

Percentile rank 0–1 useful for normalization features.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • sort_values stable?
  • rank method?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump