Filtering and OrderingΒΆ
Filtering and ordering are the most fundamental data retrieval operations β they let you ask βwhich records match my criteria?β and βin what order should I see them?β These are the Pandas equivalents of SQLβs WHERE, IN, LIKE, and ORDER BY clauses.
This notebook demonstrates boolean condition filtering (df[df['Rank'] <= 10]), membership testing with .isin(), substring matching with .str.contains(), the versatile .filter() method for selecting rows or columns by exact label or pattern, direct row access with .loc[] and .iloc[], and multi-column sorting with .sort_values() using separate ascending/descending flags per column.
import pandas as pd
df = pd.read_csv(r"C:\Users\alexf\OneDrive\Documents\Pandas Tutorial\world_population.csv")
df
df[df['Rank'] <= 10]
specific_countries = ['Bangladesh','Brazil']
df[df['Country'].isin(specific_countries)]
df[df['Country'].str.contains('United')]
df2 = df.set_index('Country')
df2
df2.filter(items = ['Continent','CCA3'], axis = 1)
df2.filter(items = ['Zimbabwe'], axis = 0)
df2.filter(like = 'United', axis = 0)
df2.loc['United States']
df2.iloc[3]
df[df['Rank'] < 10].sort_values(by=['Continent','Country'],ascending=[False,True])