Filtering and OrderingΒΆ

Filtering rows and ordering results are two of the most fundamental operations in data analysis – they are the Pandas equivalents of SQL’s WHERE and ORDER BY clauses. Mastering these techniques lets you quickly isolate the records you care about and present them in a meaningful sequence.

This notebook covers boolean condition filtering (df[df['col'] <= value]), membership filtering with .isin(), string pattern matching with .str.contains(), the .filter() method for selecting rows or columns by label, label-based access with .loc[], position-based access with .iloc[], and multi-column sorting with .sort_values(). These operations form the backbone of nearly every data exploration and reporting workflow.

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])