Pandas Fundamentals II: Sorting, Indexing, and Locating Data
March 30, 2026
nba DataFramenba:import pandas as pd
import numpy as np
# Below is for an interactive display of DataFrame in Colab
from google.colab import data_table
data_table.enable_dataframe_formatter()
# Below is to read nba.csv as nba DataFrame
nba = pd.read_csv("https://bcdanl.github.io/data/nba.csv",
parse_dates = ["Birthday"])sort_values()sort_values() methodβs first parameter, by, specifies the column(s) pandas should use to sort the rows.sort_values(ascending = False)ascending argument controls the sort direction.
True.DataFrame has many useful methods that we can combine in sequence.
nsmallest() and nlargest()nsmallest() is useful for getting the first n rows ordered by a column in ascending order.
nlargest() is useful for getting the first n rows ordered by a column in descending order.
nsmallest() and nlargest() with keep = "all"keep = "all" keeps all tied values, even if that means returning more than n rows.sort_values()DataFrame by multiple columns by passing a list to the by argument.ascending Valueascending to apply the same sort direction to every column in by.If we want different sort directions for different columns, we can pass a Boolean list to ascending.
Q. Which players on each team are paid the most?
sort_index()nba by "Name" and assigned the result back to nba. How could we restore the original row order?
nba DataFrame still keeps its numeric index labels.sort_index() sorts rows by their index labels.set_index() when we want to replace the current index of a DataFrame with one or more existing columns.
set_index()set_index() returns a new DataFrame in which the chosen column becomes the index.
keys, takes the column name.reset_index()reset_index():
DataFrame column;inplace=True, the operation modifies the original DataFrame directly.We can extract rows, columns, and individual values from a DataFrame by using the loc[] and iloc[] accessors.
.loc[Index Labels]nba with Name as the index.# The two lines below are equivalent
nba = nba.set_index("Name")
nba.set_index("Name", inplace = True).loc extracts rows by index label..loc[:] 1.loc, both the starting label and ending label are inclusive..loc[:] 2loc[:] to pull rows:
DataFrame to the end;DataFrame to a specific index label..iloc[Index Positions].iloc (index location) locates rows by their index position.
.iloc[:] follows regular Python slicing rules.
loc[Rows, Columns].loc and .iloc accept a second argument for the column(s) to extract.
.loc, we provide column names.loc[Rows, Columns] or iloc[Rows, Columns] with Integers.loc and .iloc accept a second argument for the column(s) to extract.
.iloc, we provide column positions.Letβs work on Classwork 11!