Ravindra BagaleCourses & study guides

6. Selecting, Filtering, and Sorting

6.1 Selecting Columns and Rows

import pandas as pd

df = pd.DataFrame({
    "order_id": [
        "BLK-260314-001", "BLK-260314-002", "BLK-260314-003",
        "AMN-260314-004", "BLK-260314-005", "AMN-260314-006",
        "BLK-260314-007", "BLK-260314-008",
    ],
    "city": [
        "Pune", "Nashik", "Nagpur", "Kolhapur",
        "Solapur", "Sambhaji Nagar", "Pune", "Pune",
    ],
    "amount": [64, 90, 240, 58, 1299, 195, 60, 110],
    "status": [
        "Delivered", "Delivered", "Delivered", "Cancelled",
        "Delivered", "Delivered", "Delivered", "Returned",
    ],
    "customer": [
        "Ruhi Bagale", "Amir", "Salman", "Zoya",
        "Raja", "Rani", "Shahrukh", "Ravina",
    ],
    "delivery_mins": [9, 12, 11, 14, 18, 10, 8, 13],
})
df["city"]                    # Series
df[["city", "amount"]]        # DataFrame — note double brackets
df.loc[0:2, ["city", "amount"]]
df.iloc[0:3, 0:3]             # position-based

Ravindra Bagale's Tip

Khup students df["city", "amount"] ekach brackets madhe lihitat aani KeyError yeto. Multiple columns = list inside brackets: df[["city", "amount"]]. Samajla ka? Dhyan rakho!