import pandas as pd orders = pd.DataFrame( { "order_id": [1005, 1001, 1004, 1002, 1006, 1003], "customer": ["Nia", "Ada", "Omar", "Lin", "Kai", "Maya"], "region": ["APAC", "EMEA", "EMEA", "APAC", "AMER", "AMER"], "order_date": pd.to_datetime( [ "2026-02-03", "2026-02-01", "2026-02-04", "2026-02-02", "2026-02-05", "2026-02-01", ] ), "ship_date": pd.to_datetime( [ None, "2026-02-03", "2026-02-06", "2026-02-04", "2026-02-08", "2026-02-05", ] ), "total_usd": [360.0, 150.0, 95.0, 240.0, 420.0, 875.0], } ).set_index("order_id", drop=False) print(f"pandas {pd.__version__}") print() print("BASE") print( orders[ ["customer", "region", "order_date", "ship_date", "total_usd"] ].to_string() ) print() by_total = orders.sort_values("total_usd", ascending=False) print("SORT_TOTAL_DESC") print(by_total.loc[:, ["customer", "region", "total_usd"]].to_string()) print() by_region_date = orders.sort_values( ["region", "order_date"], ascending=[True, False], ) print("SORT_REGION_DATE") print(by_region_date.loc[:, ["customer", "region", "order_date", "total_usd"]].to_string()) print() missing_ship_first = orders.sort_values("ship_date", na_position="first") print("SORT_MISSING_SHIP_FIRST") print(missing_ship_first.loc[:, ["customer", "ship_date", "total_usd"]].to_string()) print() ranked = orders.sort_values("total_usd", ascending=False, ignore_index=True) print("SORT_RESET_INDEX") print(ranked.loc[:, ["order_id", "customer", "total_usd"]].to_string()) print() by_index = orders.sort_index() print("SORT_INDEX") print(by_index.loc[:, ["customer", "region", "total_usd"]].to_string()) print() print("VERIFY") print(f"highest total order_id: {by_total.iloc[0]['order_id']}") print(f"region/date order_ids: {by_region_date['order_id'].tolist()}") print(f"missing ship_date first: {missing_ship_first.iloc[0]['ship_date']}") print(f"reset index labels: {ranked.index.tolist()}") print(f"source index order unchanged: {orders.index.tolist()}")