Thuta Learning
IntermediateData & Databasesintermediate

Selection & Indexing

Relax. We'll talk through this in plain words — no textbook voice.

Selection and Indexing

When you want to pull out a single column, a single row, or just part of a DataFrame instead of the whole thing, that's where selection/indexing comes in. In Pandas, if you know the basics of [], .loc, .iloc, that already covers most of what you need.

  • df["Column"] — selects a single column
  • df.loc[label] — selects a row by its label/index name
  • df.iloc[position] — selects a row by its numeric position
python
import pandas as pd

df = pd.DataFrame({
    "Name": ["Alice", "Bob", "Charlie"],
    "Age": [25, 30, 35],
    "City": ["New York", "Paris", "London"]
})

print(df["Name"])
print("
--- First Row ---")
print(df.iloc[0])
print("
--- Name and City only ---")
print(df[["Name", "City"]])

df["Name"] returns just the Name column as a Series. df.iloc[0] selects the first row by position. If you want to select two columns, df[["Name", "City"]] — you put the column names inside a list like this.

You should see
0 Alice 1 Bob 2 Charlie Name: Name, dtype: object --- First Row --- Name Alice Age 25 City New York Name: 0, dtype: object --- Name and City only --- Name City 0 Alice New York 1 Bob Paris 2 Charlie London

Easy traps

  • When selecting two columns, writing df["Name", "City"] can raise an error. Use double square brackets and write df[["Name", "City"]] instead.
Selection & Indexing | Thuta Learning