import pandas as pd orders = pd.DataFrame( { "order_id": ["A101", "A102", "A103", "A104", "A105"], "customer": ["Ada", "Lin", "Maya", "Omar", "Nia"], "region": ["EMEA", "APAC", "AMER", "EMEA", "APAC"], "qty": [3, 12, 7, 2, 15], "total_usd": [150.0, 240.0, 875.0, 95.0, 360.0], "status": ["paid", "paid", "open", "paid", "open"], } ).set_index("order_id") print(f"pandas {pd.__version__}") print() print("BASE") print(orders.to_string()) print() label_rows = ["A101", "A104"] label_columns = ["customer", "total_usd"] label_list = orders.loc[label_rows, label_columns] print("LOC_LABEL_LIST") print(label_list.to_string()) print() label_slice = orders.loc["A102":"A104", ["region", "qty"]] print("LOC_LABEL_SLICE") print(label_slice.to_string()) print() position_rows = [0, 3] position_columns = [0, 3] position_list = orders.iloc[position_rows, position_columns] print("ILOC_POSITION_LIST") print(position_list.to_string()) print() position_slice = orders.iloc[1:4, 1:3] print("ILOC_POSITION_SLICE") print(position_slice.to_string()) print() print("VERIFY") print(f"label rows: {label_list.index.tolist()}") print(f"position shape: {position_list.shape}") print(f"label slice includes A104: {'A104' in label_slice.index}") print(f"iloc row labels: {position_slice.index.tolist()}") print(f"source shape unchanged: {orders.shape}") print(f"A104 total by loc: {orders.loc['A104', 'total_usd']}") print(f"A104 total by iloc: {orders.iloc[3, 3]}")