Master Pandas From Scratch

Learn Pandas fundamentals, Series, DataFrames, data selection, filtering, cleaning, sorting, grouping, merging, reshaping, string operations, time series, data analysis, performance optimization, visualization, and practical projects through structured lessons.

COURSE SYLLABUS
1. Pandas Introduction and Environment Setup
Introduction to Pandas Installation and Environment Setup
2. Pandas Core Data Structures
Data Structures Overview Pandas Series DataFrames Indexes, Rows and Columns Shape, Size and Dimensions
3. Pandas Data Input and Output
Data Input and Output Overview Reading CSV, Excel and JSON Files Writing and Exporting Files
4. Initial Data Inspection
Data Inspection Overview Head and Tail Data Info and Types Descriptive Statistics
5. Selection and Indexing
Selection and Indexing Overview Column Selection loc and iloc Index Management
6. Filtering and Conditional Selection
Filtering Overview Boolean Filtering Multiple Conditions isin, between and str.contains
7. Basic Data Cleaning
Data Cleaning Overview Missing Values Duplicate Data Rename and Replace Data Type Conversion
8. Vectorized Operations and Sorting
Operations and Sorting Overview Column Operations Sorting Values and Indexes Sum, Mean, Max and Min
9. GroupBy and Aggregation
GroupBy Overview Grouping Data Aggregation Functions Group Filtering Group Transform
10. Merging, Joining and Concatenating
Merge, Join and Concat Overview Concatenation Merging DataFrames Joining by Index
11. Reshaping and Pivoting
Reshaping Overview Pivot Tables Melting Data Stack and Unstack Cross Tabulation
12. String and Categorical Data
String and Categorical Overview String Operations Categorical Data One-Hot Encoding
13. Time Series Analysis
Time Series Overview DateTime Operations Time-Based Indexes Resampling Data Shift and Rolling Windows
14. Custom Functions and Mapping
Apply and Mapping Overview Apply and Lambda Functions Map Operations Element-Wise Operations
15. Performance and Large Datasets
Performance Overview Memory Optimization Processing Files in Chunks PyArrow, Dask and Polars
16. Pandas Data Visualization
Visualization Overview Basic Plotting Histograms, Lines, Scatter and Box Plots Matplotlib and Seaborn

loc[] and iloc[] in Pandas

Learn how to select rows, columns, and individual cells using labels and integer positions in a Pandas DataFrame.

1. Introduction to loc[] and iloc[]

Pandas provides two important indexers for selecting data: loc[] and iloc[]. Both can select rows and columns, but they identify them differently.

loc[] — Label-Based Selection

Use loc[] when you want to select data using row index labels and column names.

df.loc[1]
df.loc[1, "Marks"]

iloc[] — Position-Based Selection

Use iloc[] when you want to select data using integer positions. Positions start at zero.

df.iloc[1]
df.iloc[1, 3]

2. Create a Sample DataFrame

We will use the following student dataset to understand each operation.

import pandas as pd

df = pd.DataFrame({
    "Name": ["Samadhan", "Pradnya", "Samiksha", "Amit"],
    "Age": [21, 22, 20, 23],
    "City": ["Pune", "Mumbai", "Pune", "Nashik"],
    "Marks": [85, 92, 78, 88]
})

print(df)

Output

       Name  Age    City  Marks
0  Samadhan   21    Pune     85
1   Pradnya   22  Mumbai     92
2  Samiksha   20    Pune     78
3      Amit   23  Nashik     88

The default row labels are 0, 1, 2, and 3. The column labels are Name, Age, City, and Marks.

3. Select a Row Using loc[]

To select a row by its index label, provide the label inside loc[].

print(df.loc[1])

Output

Name     Pradnya
Age           22
City      Mumbai
Marks         92
Name: 1, dtype: object

The row with index label 1 contains Pradnya's information. The result is a Series because one row was selected.

Select a row with a custom label

indexed_df = df.set_index("Name")

print(indexed_df.loc["Samadhan"])

Output

Age        21
City     Pune
Marks      85
Name: Samadhan, dtype: object
Remember: loc[] looks for the label itself. The label does not have to be an integer.

4. Select a Row Using iloc[]

The iloc[] indexer selects rows by their integer position. Since positions start at zero, position 0 is the first row and position 1 is the second row.

print(df.iloc[0])

Output

Name    Samadhan
Age           21
City        Pune
Marks         85
Name: 0, dtype: object

Select the third row

print(df.iloc[2])

Output

Name    Samiksha
Age           20
City        Pune
Marks         78
Name: 2, dtype: object
Important: iloc[2] means position 2, which is the third row. It does not search for an index label equal to 2.

5. Visual Difference Between loc[] and iloc[]

With the default index, row labels and row positions may look identical. A custom index makes the difference easier to see.

Custom Index Example Position Index Label Name Marks 0 101 Samadhan 85 1 205 Pradnya 92 2 309 Samiksha 78 loc[205] → Pradnya iloc[1] → Pradnya

Figure 1: loc[] searches for a row label; iloc[] selects a row by its position.

In this example, loc[205] selects the row labelled 205, while iloc[1] selects the second row. They happen to select the same row here, but they use different selection rules.

6. Select a Specific Column Using loc[]

Use a row label and a column name to select a particular value.

print(df.loc[1, "Marks"])

Output

92

The code returns the Marks value for the row labelled 1.

Select the name and marks for one row

print(df.loc[1, ["Name", "Marks"]])

Output

Name     Pradnya
Marks         92
Name: 1, dtype: object

7. Select a Specific Cell Using iloc[]

With iloc[], specify the row position first and the column position second. Both positions start at zero.

print(df.iloc[1, 3])

Output

92

Row position 1 is the second row, and column position 3 is the fourth column, Marks.

Select multiple columns by position

print(df.iloc[1, [0, 3]])

Output

Name     Pradnya
Marks         92
Name: 1, dtype: object

8. Select Multiple Rows with loc[]

You can pass a list of row labels to select multiple rows.

print(df.loc[[0, 2]])

Output

       Name  Age  City  Marks
0  Samadhan   21  Pune     85
2  Samiksha   20  Pune     78

Select multiple rows and specific columns

print(df.loc[[0, 2], ["Name", "Marks"]])

Output

       Name  Marks
0  Samadhan     85
2  Samiksha     78

9. Select Multiple Rows with iloc[]

Use a list of integer positions to select particular rows by position.

print(df.iloc[[0, 2]])

Output

       Name  Age  City  Marks
0  Samadhan   21  Pune     85
2  Samiksha   20  Pune     78

Select rows and columns by position

print(df.iloc[[0, 2], [0, 3]])

Output

       Name  Marks
0  Samadhan     85
2  Samiksha     78

10. Slicing with loc[] and iloc[]

Both indexers support slicing, but the end boundary behaves differently.

Slicing with loc[]

print(df.loc[0:2, ["Name", "Marks"]])

For label-based slicing, both the start and end labels are included when the labels are present and the index is appropriately ordered.

Output

       Name  Marks
0  Samadhan     85
1   Pradnya     92
2  Samiksha     78

Slicing with iloc[]

print(df.iloc[0:2, [0, 3]])

For position-based slicing, the starting position is included and the stopping position is excluded.

Output

       Name  Marks
0  Samadhan     85
1   Pradnya     92
Key difference: loc[0:2] includes label 2, while iloc[0:2] stops before position 2.

11. Visual Guide: Understanding Slicing

Slice: 0 to 2 loc[0:2] Label 0 Label 1 Label 2 Included iloc[0:2] Position 0 Position 1 Position 2 Excluded loc uses labels; iloc uses positions The stopping boundary is different

Figure 2: loc label slicing includes the ending label; iloc position slicing excludes the ending position.

12. Select Rows Using Boolean Conditions

You can also use a Boolean condition with either indexer. This is useful when you want to select rows that satisfy a condition.

With loc[]

print(df.loc[df["Marks"] >= 85, ["Name", "Marks"]])

Output

       Name  Marks
0  Samadhan     85
1   Pradnya     92
3      Amit     88

With iloc[]

mask = df["Marks"] >= 85

print(df.iloc[mask.to_numpy(), [0, 3]])

Here, the Boolean mask is converted to a NumPy array so that iloc[] receives a positional Boolean selector. In everyday filtering, loc[] is generally more convenient when the condition is built from DataFrame columns.

13. Comparison Table

Feature loc[] iloc[]
Selection type Label-based Integer-position-based
Row selector Index label Zero-based position
Column selector Column name or label Integer column position
Single row df.loc[1] df.iloc[1]
Single cell df.loc[1, "Marks"] df.iloc[1, 3]
Slice end Included for label slices Excluded for positional slices
Typical use Meaningful index labels and column names Exact row and column positions

14. Common Errors to Avoid

Error 1: Using a missing label

Selecting a label that does not exist can raise a KeyError.

Error 2: Using an invalid position

An integer position outside the valid range can raise an IndexError.

Error 3: Forgetting zero-based positions

iloc[0] is the first row, not the second.

Error 4: Confusing row labels with positions

After setting a custom index, a row label may not match its integer position. Choose the appropriate indexer based on what you intend to select.

15. Practice Exercises

Exercise 1: Row Selection

  1. Select the first row using iloc[].
  2. Select the row labelled 2 using loc[].
  3. Select the third row using iloc[].

Exercise 2: Cell Selection

  1. Retrieve Pradnya's marks using loc[].
  2. Retrieve the same value using iloc[].
  3. Select the Name and City columns for the first two rows.

Exercise 3: Slicing

  1. Select labels 0 through 2 using loc[].
  2. Select the first two rows using iloc[].
  3. Explain why the two slices return different numbers of rows.

Suggested solutions

# First row by position
print(df.iloc[0])

# Row with label 2
print(df.loc[2])

# Third row by position
print(df.iloc[2])

# Pradnya's marks
print(df.loc[1, "Marks"])
print(df.iloc[1, 3])

# First two rows and selected columns
print(df.loc[0:1, ["Name", "City"]])

# Label-based slice
print(df.loc[0:2])

# Position-based slice
print(df.iloc[0:2])

16. Revision Questions

  1. What is label-based selection?
  2. What is position-based selection?
  3. What is the difference between loc[] and iloc[]?
  4. How do you select a specific cell using each method?
  5. What is the difference between loc[0:3] and iloc[0:3]?
  6. Why is iloc[0] the first row?
  7. How can you select multiple rows and columns together?
  8. What errors can occur when selecting missing labels or invalid positions?

Summary

The loc[] indexer selects rows and columns using labels, while iloc[] selects them using integer positions. Both methods can retrieve individual rows, specific cells, multiple rows, and selected columns.

Remember that label-based slices generally include their ending label, whereas position-based slices exclude the ending position. Choosing the correct indexer makes your Pandas code more accurate and easier to understand.